Hi everybody,

I'm developping a module for managing a form. I need to stay in the same page after submitting (for triggering with a js). But when i use $form_state['redirect'] = FALSE; (in every function), the process is executed two time. For example, i use form submit like this:

function commande_express_form_submit($form, &$form_state) {
  $form_state['redirect'] = FALSE;
	
  db_insert('commande_express')
    ->fields(
      array(
        'date' => date('Y-m-d__H:i:s'),
        'nom' => $form_state['values']['name'],
        'telephone' => $form_state['values']['phone'],
        'dispo' => $form_state['values']['dispo']
      )
    )->execute();
	
  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
}

I don't know why but with this code, db_insert is executed two time, and there is two same line in my database. And then the drupal_add_js is executed two time too and i get two alert("Hello") in my browser.

Thanks you for you help.

Comments

blueminds’s picture

Does the double submittion happen even without $form_state['redirect'] = FALSE;

modou33’s picture

No, the code is submitted one time when i usen't the $form_state['redirect']=FALSE and all work nice.

For every hook function, when i call a $form_state['redirect'], the code in the hook is called two times. For exemple:

function commande_express_form_alter(&$form, &$form_state, $form_id) {
  $form_state['redirect'] = FALSE;
  drupal_add_js('jQuery(document).ready(function () { alert("In form alter!"); });', 'inline');
}

I got two echo alert with this code.

Web Assistant’s picture

Did you mean you were triggering the form submit with JS. If so, do you have the code for that?

modou33’s picture

My form is processed by drupal classic system, but after the end of submit process, i just need to stay in the same page for updating some block.

modou33’s picture

here is all my code in commande_express.module

<?php

/**
 * Implements hook_block_info().
 */
function commande_express_block_info() {
..// This example comes from node.module.
..$blocks['commande-express-widget-block'] = array(
......'info' => t('Commande expresse widget'),
......// DRUPAL_CACHE_PER_ROLE will be assumed.
..);

..return $blocks;
}

/**
 * Implements hook_block_view().
 */
function commande_express_block_view($delta = 'commande-express-widget-block') {
  // This example is adapted from node.module.
..$block = array(
......'subject' => NULL,
......'content' => '<div class="widget">'
.........'<h2>'. t('Commande expresse') . '</h2>'
.........'<p>' . t('Vous souhaitez commander? laissez vos coordonn&eacute;es, nous vous contactons pour faire une composition ensemble.') . '</p>'
........ render(drupal_get_form('commande_express_form'))
........'</div>'
..);

  return $block;
}

/**
 * Implements my_form_form($form_state).
 */
function commande_express_form($form_state) {
..$form['name'] = array(
......'#type' => 'textfield',
......'#title' => t('Nom'),
......'#size' => 20,
......'#required' => TRUE
..);

..$form['phone'] = array(
......'#type' => 'textfield',
......'#title' => t('T&eacute;l&eacute;phone'),
......'#size' => 20,
......'#required' => TRUE
..);

..$form['hContact'] = array(
......'#type' => 'fieldset',
......'#title' => t('Horraires de disponibilit&eacute; pour &ecirc;tre rappeler (cliquez pour d&eacute;plier):'),
......'#collapsible' => TRUE,
......'#collapsed' => TRUE,
..);

..$form['hContact']['dispo'] = array(
......'#type' => 'textarea',
......'#description' => t('Indiquez une plage horraire. Exemple: entre 09h00 et 11h30')
..);

..$form['submit'] = array(
......'#type' => 'submit',
......'#value' => 'Valider',
..);
..
..return $form;
}

/**
 * Implements myform_validate($form, &$form_state).
 */
function commande_express_form_validate($form, &$form_state) {
../*Vérification de la validité du numéro de téléphone: contient 10 chiffre et
.. * commence par 0 */
..$phone = trim($form_state['values']['phone']);
..if (strlen($phone) != 10 || !is_numeric($phone) || $phone[0] != '0' ) {
....form_set_error('phone', t('Le num&eacute;ro de t&eacute;l&eacute;phone doit contenir 10 chiffres et commencer par le chiffre 0.'));
....drupal_add_js('jQuery(document).ready(function () { alert("'. t('Attention: vous n\'avez pas saisi un num&eacute;ro de t&eacute;l&eacute;phone valide.'). '"); });', 'inline');
..}
}

/**
 * Implements myform_submit($form, &$form_state).
 */
function commande_express_form_submit($form, &$form_state) {
..$form_state['redirect'] = FALSE;
..
..db_insert('commande_express')
..->fields(
......array(
..........'date' => date('Y-m-d__H:i:s'),
..........'nom' => $form_state['values']['name'],
..........'telephone' => $form_state['values']['phone'],
..........'dispo' => $form_state['values']['dispo']
......)
..)->execute();
..
/*test js just after submitting*/
..drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');

}