2

I would like to implement something like the second example in this link but not when the form is submitted, actually when a radio button is clicked. (click to alert) http://d7.drupalexamples.info/examples/ajax_example/advanced_commands. With the code below i managed to alert the message but after that the form does not work properly, after the submit it redirects me to $base_url/system/ajax and a json array is displayed.

$form['submitted']['test']['#ajax'] = array
        (
           'callback' => '_test',
           'wrapper' => 'nimportequi',
           'progress' => array('type' => 'throbber', 'message' => '')
        );

and the callback:

function _test($form, $form_state){
    $commands = array();
     $text = "";
     $alert = 0;
     if (valid_email_address($form['submitted']['email']['#value']) !== FALSE)
     {
        $result = user_load_by_mail($form['submitted']['email']['#value']);
        $text = ($result !== FALSE)?$alert=1:"Based on your email, you are not a member yet!";
     }
     else 
        $text = "Based on your email, you are not a member!";
     if ($alert != 1 && $form_state['values']['submitted']['member'] == 1)
     {
        $commands[]=ajax_command_alert($text);
       // $commands[] = ajax_command_data("#foo", 'label_radio', '1');
     }
     $form_state['rebuild'] == TRUE;
     return array('#type' => 'ajax', '#commands' => $commands);
}

Also i am using a rule when the form is submitted. What am i doing wrong ? I cannot figure it out…

  • can you post what you have in the javascript firebug console when you click on the submit? Maybe there are some indications there – Mika A. Jan 23 '13 at 12:42
  • @MikaA. there's nothing. As i said the alert window is fired but the form does not work properly after i hit ok on the alert window. If i click submit it gets me to system/ajax... – Vlasin Jan 23 '13 at 12:43
  • have not found an answer yet! – Vlasin Jan 30 '13 at 9:54
3

Add the ajax callback in the submit button of the form like this:

Edit

$form['radio_buttons']['#ajax'] = array (
  'callback' => '_test',
  'wrapper' => 'nimportequi',
  'event' => 'change',
  'progress' => array('type' => 'throbber', 'message' => ''),
);

This will ensure that when a radio button is clicked your ajax callback will be executed. Selecting radio buttons will give alert message, therefore the ajax callback should be applied to radio buttons.

Your ajax callback seems ok.

  • thank you for the answer , but that's not what i want to do . The ajax callback should be placed on some radio buttons , because after someone selects an option i want to alert him with a message based on another value from the form . The problem is that the alert box if fired and after the alert is displayed on form submission it redirects me to system/ajax, which is wrong. Also i forgot to say that i am using rules for that form . Could this be a problem ? – Vlasin Jan 24 '13 at 8:11
  • what you are doing in that rule? – subhojit777 Jan 24 '13 at 8:24
  • adds some nodes , redirects and shows a message ! – Vlasin Jan 24 '13 at 8:48
  • not sure, may be the redirect that you are doing in the rule is causing the error. for debug purpose disable the rule and submit the form – subhojit777 Jan 24 '13 at 8:54
  • i don't think that the rule is the problem , because i have a captcha on that form , and what i am trying to do is submit the form with wrong captchas and still the same thing happens , redirects me to system/ajax. – Vlasin Jan 24 '13 at 8:57
0

You need to set the '#ajax' on the select form element, not the submit.

eg.

$form['myselect'] = array(
  '#type' => 'select',
  '#options' => array(1, 2),
  '#ajax' => array(
    'callback' => '_test',
    'event' => 'change',
  ),
);

function _test($form, $form_state) {
  $commands = array();
  $commands[]=ajax_command_alert('testing');
  return array('#type' => 'ajax', '#commands' => $commands);
}
  • Woops.....I'm drunk right now.....haha...well...now you know how to do it with a slect too :) – 2pha Jan 25 '13 at 13:52
0

The problem was that i had a call like :

$form['#action'] = $_SERVER['REQUEST_URI'];

and this was causing that after second submit the variable will take the value:

$form['#action'] = 'system/ajax';

changing this to correct behavior solved my issue like below.

 if (!isset($form_state['values']))
    $form['#action'] = $_SERVER['REQUEST_URI'];
else
    $form['#action'] = $custom_url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.