0

I have a drupal form which accept email id and ajax call is implemented for the submit button.In addition to submit call back.The return values are collected in the wrapper and my requirement is to display the form along with an error message if the email is not valid.

I tried to return the form along with message to the wrapper from submit,but the ajax call was not happening.

Also i tried to reset the form_state array empty in the submit call back, but execution stopped at that point.

Am i on the correct track? Please help me in solving this.Sorry for my english.

1

For Drupal 7 you can use the below code


    /**
     * Implements hook_menu().
     */
    function hook_menu() {
        $items['test_ajax'] = array(
            'title' => 'test ajax',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('demo_form'),
            'access callback' => TRUE,
            'type' => MENU_CALLBACK,
        );
            ....
    }   

    /**
     * Implements hook_form().
     */
    function demo_form($form, &$form_state) {
        return array(
            'email' => array(
                '#type' => 'textfield',
                '#title' => t("Join our Newsletter"),
                '#required' => TRUE,
                '#attributes' => array(
                    'placeholder' => t("mail@example.com")
                ),
            ),
            'submit' => array(
                '#type' => 'submit',
                '#value' => t('Subscribe'),
                '#ajax' => array(
                    'callback' => 'demo_form_ajax_submit',
                    'wrapper' => 'demo-form',
                    'method' => 'replace',
                    'effect' => 'fade',
                ),              
            ),
        );
    }

    /**
     * Ajax callback function.
     */
    function demo_form_ajax_submit($form, $form_state) {
        drupal_set_message();
        $message = '';
        if (!empty($form_state['input']['email'])) {
            if (valid_email_address($form_state['input']['email'])) {
                $message = "subscription successful!!!";
                $status = 'status';
            }
            else {
                $message = 'Please enter valid email ID!!';
                $status = 'error';
            }
            drupal_set_message($message, $status);
        }
        $form_state['rebuild'] = TRUE;
        return $form;
    }


  • Hi @inizio ,thanks very much for the response.I tried by setting form_state['rebuild'] = TRUE ,but wrapper is showing Array instead of form. – Vishnu May 27 '14 at 9:57
  • Have you tried my code? Wrapper is showing Array instead of form means? – inizio May 27 '14 at 10:01
  • ,i tried your code.yaaa wrapper is showing as Array instead of form – Vishnu May 27 '14 at 10:41
0
function demo_form_ajax_submit($form, &$form_state){
  $commands = array();

  if($form_state['values']['status']){ // some validations
      $commands[] = ajax_command_html('#id-form', theme('sussces_page'));
  }
  else{
      $commands[] = ajax_command_html('#message-error', theme('status_messages')); // message-error is a div defined in your form
  }
  return array('#type' => 'ajax', '#commands' => $commands);
}
  • Please avoid code-onlt answers – Mołot Sep 30 '15 at 8:15

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.