There are certain conditions (and I can't figure out exactly what they are) where the forms API will properly display a form and execute the directives for processing that form, but when hitting the _submit function it does not pass the values of the form on.
I want to be clear here, it IS executing all the right procedures and it IS passing the array $form_values, with keys matching the fields specified in the form declaration, but it is removing the values associated with each key, making it look as though the form was submitted completely blank. However, $_POST['edit'] still contains ALL the correct key=>value pairs.
The only thing I was able to concretely establish was that I could only make this happen when the function that generates the form is DIRECTLY pointed to by a menu item in the _menu hook.
Comments
Comment #1
beginner commentedCould you tell us how to reproduce on a fresh install of Drupal?
Comment #2
Logi-1 commentedHi,
I am having the same problem. I am running Drupal 4.7.
Nota bene, I am doing my first form.
But the $form_values always comes up empty and the $_POST[edit] has all values. I am also coming directly from a menu item.
This was not a good way to start using forms!
regards, Logi
Comment #3
beginner commentedWell, can you post some code so that we can reproduce the problem?
Comment #4
Logi-1 commentedI sure can, but I have to stress that this is my first form and there might just be a mistake in it. Still the $_POST[edit] gets set and not the $form_variables. Here is some of it (I'm trying to get contact_dir to work again):
function _contact_dir_search_form()
{
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('contact name'),
'#size' => 30,
'#maxlength' => 100,
'#description' => t('All or part of the contact name'),);
$form['category'] = array(
'#type' => 'select',
'#title' => t('category'),
'#default_value' => '',
'#options' => _contact_dir_get_select_category(true),
'#description' => t("Here you can select a category to narrow down the search"),);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),);
$output = drupal_get_form('search', $form);
return $output;
}
Then we have the submit handling:
If I change the $_POST[edit][name] to $form_values['name'] it does nei work anymore...
function search_submit($form_id, $form_values)
{
$header = array();
$colspan = 0;
$simi = utf8_encode('Símanúmer');
$header[] = array('data' => t('Nafn'), 'field' => 'name', 'sort' => 'asc');
$header[] = array('data' => t('Starfsheiti'), 'field' => 'job');
$header[] = array('data' => t('Mail'), 'field' => 'mail');
$header[] = array('data' => $simi, 'field' => 'phone');
$colspan = 4;
$sql = "SELECT co.cid, CONCAT_WS(' ', co.first_name, co.last_name) AS name, ";
$sql .= "co.job as job, co.mail as mail, co.phone as phone, co.uid as uid ";
$sql .= "FROM {contact_directory} co, {contact_category} ca, {users} u ";
$sql .= "WHERE co.category = ca.cid ";
$sql .= "AND co.uid = u.uid ";
if ( $_POST[edit][name] )
{
$param = $_POST[edit][name];
$sql .= "AND CONCAT_WS(' ', co.first_name, co.middle_name, co.last_name) like '%$param%' ";
}
if ( $_POST[edit][category] )
{
$param = $_POST[edit][category];
$sql .= "AND co.category = $param ";
}
if ( $uid )
{
$sql .= "AND co.uid = $uid ";
}
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$rows = array();
$pager = theme('pager', NULL, 50, 0);
if (!empty($pager))
{
$rows[] = array(array('data' => $pager, 'colspan' => $colspan));
}
if ($interface == 'admin')
{
while ($contact = db_fetch_object($result))
{
$rows[] = array($contact->cid,
$contact->name,
$contact->mail,
$contact->category,
l(t('view'), 'contact_dir/'. urlencode($contact->cid)));
}
}
else
{
while ($contact = db_fetch_object($result))
{
$mailtomass = "mail\">$contact->mail";
$mailscrambled = makeBotFreeEmail($contact->mail);
$phonesplit = substr($contact->phone,0,3)." ".substr($contact->phone,3,4);
$rows[] = array(l($contact->name, 'contact_dir/'. urlencode($contact->cid)),
$contact->job,
$mailscrambled,
$phonesplit,
l($contact->username, 'user/'. urlencode($contact->uid)));
}
}
return theme('table', $header, $rows);
}
Comment #5
Logi-1 commentedSorry that was supposed to be:
I sure can, but I have to stress that this is my first form and there might just be a mistake in it. Still the $_POST[edit] gets set and not the $form_values. Here is some of it (I'm trying to get contact_dir to work again):
..
..
..
Comment #6
heine commentedPlease try again with the code between <?php ?> tags.
Comment #7
heine commentedIncidentally, see http://drupal.org/node/62304
Comment #8
Logi-1 commentedTHANK you for that security pointer, I was wondering about that before.
Here it is again, the code was missing when I resubmitted:
I sure can, but I have to stress that this is my first form and there might just be a mistake in it. Still the $_POST[edit] gets set and not the $form_values. Here is some of it (I'm trying to get contact_dir to work again):
function _contact_dir_search_form()
{
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('contact name'),
'#size' => 30,
'#maxlength' => 100,
'#description' => t('All or part of the contact name'),);
$form['category'] = array(
'#type' => 'select',
'#title' => t('category'),
'#default_value' => '',
'#options' => _contact_dir_get_select_category(true),
'#description' => t("Here you can select a category to narrow down the search"),);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),);
$output = drupal_get_form('search', $form);
return $output;
}
Then we have the submit handling:
If I change the $_POST[edit][name] to $form_values['name'] it does nei work anymore...
function search_submit($form_id, $form_values)
{
$header = array();
$colspan = 0;
$simi = utf8_encode('Símanúmer');
$header[] = array('data' => t('Nafn'), 'field' => 'name', 'sort' => 'asc');
$header[] = array('data' => t('Starfsheiti'), 'field' => 'job');
$header[] = array('data' => t('Mail'), 'field' => 'mail');
$header[] = array('data' => $simi, 'field' => 'phone');
$colspan = 4;
$sql = "SELECT co.cid, CONCAT_WS(' ', co.first_name, co.last_name) AS name, ";
$sql .= "co.job as job, co.mail as mail, co.phone as phone, co.uid as uid ";
$sql .= "FROM {contact_directory} co, {contact_category} ca, {users} u ";
$sql .= "WHERE co.category = ca.cid ";
$sql .= "AND co.uid = u.uid ";
if ( $_POST[edit][name] )
{
$param = $_POST[edit][name];
$sql .= "AND CONCAT_WS(' ', co.first_name, co.middle_name, co.last_name) like '%$param%' ";
}
if ( $_POST[edit][category] )
{
$param = $_POST[edit][category];
$sql .= "AND co.category = $param ";
}
if ( $uid )
{
$sql .= "AND co.uid = $uid ";
}
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$rows = array();
$pager = theme('pager', NULL, 50, 0);
if (!empty($pager))
{
$rows[] = array(array('data' => $pager, 'colspan' => $colspan));
}
if ($interface == 'admin')
{
while ($contact = db_fetch_object($result))
{
$rows[] = array($contact->cid,
$contact->name,
$contact->mail,
$contact->category,
l(t('view'), 'contact_dir/'. urlencode($contact->cid)));
}
}
else
{
while ($contact = db_fetch_object($result))
{
$mailtomass = "mail\">$contact->mail";
$mailscrambled = makeBotFreeEmail($contact->mail);
$phonesplit = substr($contact->phone,0,3)." ".substr($contact->phone,3,4);
$rows[] = array(l($contact->name, 'contact_dir/'. urlencode($contact->cid)),
$contact->job,
$mailscrambled,
$phonesplit,
l($contact->username, 'user/'. urlencode($contact->uid)));
}
}
return theme('table', $header, $rows);
}
Comment #9
jvandyk commentedCannot reproduce.
Comment #10
lapurd2006 commentedI have had the same problem. You can reproduce in the following steps:
1. create a drupal form with a hidden type form item, give it a default value.
2. use javascript to change this form item value to a new value.
3. when submit this form, if you use $form_values[form_id], you will find that it still gets the default
value, however, if you use $_POST['edit'][form_id], it gets the new value.
Comment #11
heine commented@ lapurd,
Try again with the elements property '#DANGEROUS_SKIP_CHECK' set to TRUE
Comment #12
chx commentedyou sure using #default_value and not #value?
Comment #13
oadaeh commentedI'm not entirely sure my problem is related to this one, but it seems to be, so I'm adding to it. This is the code, as simple as I could get it, that displays the problem for me. There is nothing in $form_values, but there is in $_POST. It's entirely possible I'm doing something wrong, but it was what I came up with based on the documentation I could find about it on d.o and a.d.o.
Comment #14
RobRoy commentedYou're not actually using $form_values in fapitest_form(). Use the #default_value attribute like chx recommended.
http://api.drupal.org/api/4.7/file/developer/topics/forms_api.html
Comment #15
chx commented