0

How to fetch form state value of a form on clicking custom mobile verification(required field in register form) button ajax based suppose I want to show mobile number(Form_state value) of form on clicking ajax based button named mobile verification. Here's the code:

 if($form_id == 'user_register_form')
 {

        $form['mybutton'] = array(
        '#type' => 'button',
        '#name' => 'mybutton',
        '#value' => 'Mobile Verification',
        '#title' => t('Mobile Verification'),
        //'#submit' => array('my_ajax_callback'),
        '#prefix' => '<div id="dialog" title="Dialog Title" style="display:none;"><span class="close">X</span><div ><form action="#">
                        Enter OTP<br>
                        <input type="text" class="otp" name="otp" value="">
                        <br>
                        <input type="submit" class="otp_submit" value="Submit">
                        </form> </div></div>',
        '#weight' => 19,
        '#ajax' => array(
                'event' => 'click',
                'callback' => 'my_ajax_callback',
                'method' => 'replace',                  
                'wrapper' => 'form-wrapper',
          ),
        );


}

function my_ajax_callback(&$form, &$form_state,$form_id) {

     echo '<pre>';      
     print_r($form_state);  
     die;
   // return $form;
 }  

In short i just want without saving form data can we show form_state value on clicking ajax based button in popup form.

The above code not working for me not able to get form_state value

2
  • The question isn't clear, as you already are using $form_state, and it doesn't need to be fetched.
    – Jaypan
    Jan 11, 2017 at 9:48
  • In short i just want without saving form data can we show form_state value on clicking ajax based button in popup form
    – user59414
    Jan 11, 2017 at 9:54

1 Answer 1

1

In that case, you can create a container in the form, and fill it with the values after AJAX has been fired. Then you only need to return that container in your ajax callback.

if($form_id == 'user_register_form')
{
  $form['mybutton'] = array(
    // ...
    '#ajax' => array(
      'callback' => 'my_ajax_callback',
      'wrapper' => 'form_state_values_wrapper',
    ),
  );

  $form['form_state_values'] = array(
    '#prefix' => '<div id="form_state_values_wrapper">',
    '#suffix' => '</div>',
    '#markup' => '',
  );

  if(isset($form_state_, $form_state['values']))
  {
    $form['form_state_values']['#markup'] = '<pre>' . print_r($form_state['values'], TRUE) . '</pre>',
  }

}

function my_ajax_callback(&$form, &$form_state,$form_id) {
  return $form['form_state_values'];
}
2
  • i think something is missing in your code i am getting blank page after submitting button
    – user59414
    Jan 11, 2017 at 10:18
  • Update your code with your full code.
    – Jaypan
    Jan 11, 2017 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.