Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By c_m_b on
Hello,
I want to use AJAX to submit a form & replace some content while the code runs in the background. I've made a form that works but if I ad the AJAX callback it no longer actions the form.
This may be really obvious but I can't seem to get it working, is there a trick to it?
Here is a simplified version of what I'm trying to do. Any feedback is appreciated.
function trial_registration_form($form, &$form_state) {
$form = array();
$form['#method'] = 'post';
$form['#action'] = url(register.php');
$form['#suffix'] = '<div id="form-register-message"></div>';
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
);
// submit
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#ajax' => array(
'callback' => 'trial_registration_form_submit',
'wrapper' => 'form-register-message'
),
'#submit' => array('trial_registration_form_submit'),
);
return $form;
}
function trial_registration_form_submit($form, &$form_state) {
// create message
$content = t('Thank you for registering. An email with login details will be sent to you shortly.');
// Process form submission.
$commands = array(
ajax_command_replace('#form-register-wrapper', $content),
);
$replace = array('#type' => 'ajax', '#commands' => $commands);
return $replace;
}
Comments
Did you get the answer
Hi,
I have a similar requirement. Please post back here if you get the answer for the above query posted.
Thanks,
Kiran
Try this
Try this:
This works for the ajax submit. This will replace the form with our custom message.
Changing the #action of the
Changing the #action of the link will cause the ajax to fail. Why are you changing the action? You should almost never have to do this in Drupal.