drupal form submit not doing as expected - forms

I am trying to submit a form using the submit button created like this:
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 5,
'#submit' => array('edit_form_submit'),
);
However when the form is submit the
function edit_form_submit($form, &$form_state){
dsm('IM HERE!!!');
}
does not get run.
I have checked the usual things line:
the $form['#form_id'] is 'edit_form' and $form['#type'] is set to 'form'.
I'm not quite stuck on this. I do think it must be a simple overlooked problem but I dont see it.
Any Ideas??
If you require more information please ask.

You cant do it in this way ,
you have to do something like this
function mymodule_myfrom($form, &$form_state){
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 5,
);
return $form;
}
function mymodule_myfrom_submit($form, &$form_values){
dsm('IM HERE!!!');
}
To call your form do this
$myfrom=drupal_get_form('mymodule_myfrom');
print drupal_render($myfrom);
to see complete example check out this example module, you can find from example
https://drupal.org/project/examples
Also you can check tutorials like this one
http://mrphp.com.au/blog/how-make-simple-form-module-drupal

Related

Drupal 7: Show fields on a form based on another field and a database lookup

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.

#value not being changed in custom drupal form

I have created a custom drupal 7 form with a submit button. when submit is pressed the values in $form_state['values'] are not changed. see the code below:
$form[$tag] = array(
'#title' => t($tag),
'#type' => 'textfield',
'#default_value' => !empty($form_state['values'][$tag]) ? $form_state['values'][$tag] : $value,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 5,
'#submit' => array('xml_form_builder_edit_datastream_form_submit'),
);
Then the submit function is:
function xml_form_builder_edit_datastream_form_submit($form, &$form_state){
dsm('SUBMITTED');
dpm($form);
dpm($form_state);
dsm('SUBEND');
}
the $form_id is 'xml_form_builder_edit_datastream_form'.
Ive been trying to figure this out for a good few hours.
Anyone got any ideas?
Try $form_state['input'][$tag] , that should have the user input value.

sending form values $form_state['redirect'] Form API Drupal

I want to send my form values using form API drupal. I have following value
$form['billing']["cardholders_name"] = array(
'#type' => 'textfield',
'#title' => t("Cardholder's Name"),
'#required' => TRUE,
'#prefix' => '<div class="field-wrapper-w1 card-name">'
);
I am writing following code in my form submit function
function test_form_submit($form, &$form_state) {
$form_state['redirect'] = 'www.test.com/page' . '?cname=' . $form_state['values']['billing[cardholders_name]'];
}
But it seems like it is not working. Please help
The following example appears in
http://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7
$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);
Any value for form element will be in $form_state under values so in your function test_form_submit you can access cardholders_name
$form_state['values']['cardholders_name']
also you can do it in this way using drupal_goto()
drupal_goto('www.test.com/page' . '?cname=' . $form_state['values']['cardholders_name]);

In Drupal 7, how do I show a new form that can edit data selected from a different form?

I have a tableselect form that lists several items. When a user selects one or more items, and clicks the edit button, I want a new form to show up that lets them edit the items.
I have the new form structure being generated, but I can't get it to show up after the edit button is clicked.
Currently, nothing seems to happen. I know that the tableselect form is being submitted correctly, and the function to create the edit term form is working correctly. I tested it with drupal_set_message and var export.
So, how do I get the new form to show?
Here is my relevant code:
/**
* Generate form for listing terms
*/
function markit_form_terms_list()
{
$form = array();
$form['terms'] = array(
'#type' => 'fieldset',
'#title' => t('List of Terms'),
);
$header = array(t('Name'), t('ID'), t('SetID'));
$form['terms']['items'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#title' => t('Terms'),
'#options' => markit_get_array_terms(),//drupal_map_assoc($header, 'markit_get_array_terms'),
'#tree' => TRUE,
);
$form['terms']['submit'] = array(
'#type' => 'submit',
'#value' => t('Edit Term'),
'#submit' => array('markit_form_terms_list_submit'),
);
/*$form['terms']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete Term'),
'#submit' => 'markit_form_terms_delete'
);*/
return $form;
}
/**
* Generate form to edit the terms.
* #param <type> $form
* #param <type> $form_state
* #return string
*/
function markit_form_term_edit($form, $form_state)
{
$newform = array();
$newform['termstoedit'] = array(
'#type' => 'fieldset',
'#title' => t('Edit Term/s'),
);
foreach($form_state['values']['items'] as $row)
{
if($row!=0)//if a row is not selected, it will be 0. So don't select rows equal to 0.
{
$terminfo = markit_get_markterms($row);
drupal_set_message(var_export($terminfo,true));//['term_name']);
drupal_set_message($terminfo[0]['term_name']);
$newform['termstoedit'][$terminfo[0]['term_id']] = array(
'#type' => 'textfield',
'#title' => t('Term:'),
'#default_value' => $terminfo[0]['term_name'],
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
);
}
}
$newform['termstoedit']['submit'] = array(
'#type' => 'submit',
'#value' => t('Edit Term'),
'#submit' => array('markit_form_term_edit_submit'),
);
drupal_set_message(var_export($newform,true));
return $newform;
}
function markit_form_terms_list_submit($form,$form_state)
{
drupal_set_page_content(drupal_build_form('markit_form_term_edit', $form_state));
}
I believe I am not using the correct code in the markit_form_terms_list_submit function. I've tried several different things, but it hasn't worked yet. And the Google searches I've done haven't helped either. I also have searched the Drupal API and Drupal Form API sites extensively.
Anyway, I think that's all the info you might need in order to help me. Thanks in advance!
I believe you may have better luck if you have one form function with differing fields being shown depending on the output.
If you do this you will even be able to use the form ajax methods to auto update your form.
Have a look at this howto to see if you think the approach will work for you.

drupal questions - how to print a form from another module?

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.
In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:
// Broadcast tab on group node.
$items['node/%node/broadcast'] = array(
'title' => 'Broadcast',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_broadcast_form', 1),
'access callback' => 'og_broadcast_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 7
);
And in og.pages.inc, the function is:
function og_broadcast_form($form_state, $node) {
drupal_set_title(t('Send message to %group', array('%group' => $node->title)));
if (!empty($form_state['post'])) {
drupal_set_message(t('Your message will be sent to all members of this group.'));
}
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 70,
'#maxlength' => 250,
'#description' => t('Enter a subject for your message.'),
'#required' => TRUE,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#rows' => 5,
'#cols' => 90,
'#description' => t('Enter a body for your message.'),
'#required' => TRUE
);
$form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
$form['gid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
I've tried a number of variations of drupal_get_form:
print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n);
etc., etc... Is there a way to accomplish what I'm trying to do here?
FYI.. your problem is that you're attempting to load a form located in another modules include file.
The function is located in og.pages.inc and you'll need to make a call to:
module_load_include('inc', 'og', 'og.pages');
This must be done before you can grab the form.
If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.
On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.
You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.
Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.
EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.
drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?
drupal_get_form() only accepts one argument, the $form_id.
http://api.drupal.org/api/function/drupal_get_form/6
Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.