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

beginner’s picture

Version: 4.7.0 » x.y.z

Could you tell us how to reproduce on a fresh install of Drupal?

Logi-1’s picture

Hi,

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

beginner’s picture

Well, can you post some code so that we can reproduce the problem?

Logi-1’s picture

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_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);

}

Logi-1’s picture

Sorry 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):

..
..
..

heine’s picture

Please try again with the code between <?php ?> tags.

heine’s picture

Logi-1’s picture

THANK 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);

}

jvandyk’s picture

Cannot reproduce.

lapurd2006’s picture

Priority: Normal » Critical

I 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.

heine’s picture

Priority: Critical » Normal

@ lapurd,

Try again with the elements property '#DANGEROUS_SKIP_CHECK' set to TRUE

chx’s picture

you sure using #default_value and not #value?

oadaeh’s picture

Version: x.y.z » 5.1

I'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.

function fapitest_menu($may_cache) {
  $items = array();

  $items[] = array(
    'path'     => 'fapitest',
    'title'    => 'Forms API test',
    'access'   => user_access('access content'),
    'callback' => 'fapitest_page');

  return $items;
}

function fapitest_page() {
  // This line doesn't change the form's behavior:
  global $form_values;

  $output  = "From form_values: The text you entered was: '". $form_values['fapitesttext'] ."'<br />";
  $output .= "From _POST: The text you entered was: '". $_POST['fapitesttext'] ."'<br />";

  $output .= drupal_get_form('fapitest_form', $form_values);

  return $output;
}

function fapitest_form($form_values = NULL) {
  // This line doesn't change the form's behavior:
  $form['#redirect'] = FALSE;

  $form['fapitesttext'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to be displayed'));

  $form['fapitestbutton'] = array(
    '#type' => 'button',
    '#title' => t('Submit'));

  return $form;
}
RobRoy’s picture

Category: bug » support

You'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

chx’s picture

Status: Active » Closed (fixed)