سؤال

I'm trying to submit a form and use hook_form_submit.

The problem is the form is displayed via ajax and this results in hook_form_submit not being called.

$items['ajaxgetform/%'] = array(  
  'page callback' => 'ajaxgetform',  
  'access arguments' => array('access content'),  
  'type' => MENU_CALLBACK  
);   

function ajaxgetform($form_id) {    
  drupal_get_form($form_id);  
  return drupal_json($panel);  
}  

function_myform_form($form_state) {  
  $form['myform'] = array(  
    '#title' => 'myform value',  
    '#type' => 'textfield',  
    '#default_value' => 'myform default value'  
  );  

  $form['#action'] = url('myurl');

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

  $form['#ajaxsubmit'] = TRUE;  
    return $form;  
  }  

hook_form_alter() does get called.

Below doesn't get called?

function myform_form_submit($form, $form_state) {   
  // ...  
} 

I'm not sure if this is a common problem, but i've been stuck for hours trying to make it work.

If I remove $form['#action'] = url('myurl'); myform_form_submit() gets called. However I get a white screen with jason script.

هل كانت مفيدة؟

المحلول

There is no hook_form_submit(). Instead, you register submit handlers with $form['#submit']. So, if you want to call myform_form_submit() when the form gets submitted, add:

$form['#submit'][] = 'myform_form_submit';

to myform_form(). Take a look at the 5.x to 6.x form changes and the Forms API reference for more info.

نصائح أخرى

Is your form displayed on the page at myurl ? In order for a form submission to be processed, the form as to be displayed (using drupal_get_form()) on the page used as action.

You may also try to se the form #redirect to the landing page URL instead of its #action. This way, the form is submitted to its generating URL but the user is redirected to your destination page after processing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top