0

I am totally new to Drupal Ajax. In my projects one simple form with submit button only there for insert "yes" value to Database. so it should be done with Drupal Ajax. So When that button submit the value should be store in Db with out page load and after submit display text " successfully selected" instead of button.

So please anyone can help me for this task

2 Answers 2

1

First of all I want to point you to the AJAX Example module. This has some neat examples on how to use it.

For your task what you basically need is the following:

  1. Replace normal submit handler by custom submit handler
  2. Add an AJAX callback and wrapper to your submit button
  3. Do your database queries in the submit handler, force rebuilding the form and define the message.
  4. Replace the submit button of your form with a new one and show the message.

I cannot test the following code right now but it should basically give you the idea:

<?php

function MY_MODULE_form($form, &$form_state) {
  // some form elements

  $form['submit'] = array(
    '#type' => 'submit',
    '#submit' => array('MY_MODULE_form_custom_submit'), // define custom submit handler
    '#value' => t('Submit'),
    '#ajax' => array(
      'callback' => 'MY_MODULE_form_callback', // define ajax callback to show the message
      'wrapper' => 'my-submit-button', // define what div should be replaced
    ),
    '#prefix' => '<div id ="my-submit-button">', // wrap this button in the div that is replaced
    '#suffix' => '</div>',
  );
}

function MY_MODULE_form_custom_submit($form, &$form_state) {
  // do your db query
  drupal_set_message(t('Something has been done!')); // your message

  $form_state['rebuild'] = true; // rebuild the form. not sure if this is required now.
}

function MY_MODULE_form_callback($form, &$form_state) {
  return $form['submit'];
}

?>

Hope this helps. If the code does not work as it should let me know and I will test it asap.

cheers, j

7
  • Hello schlicki, thanks for your Reply will test it and let you know.
    – Anaz
    Feb 24, 2015 at 8:45
  • schlicki.. thanks for your reply it was working.but i am struggling in after submit i want to change the button name. example: "Add Friend" button should be change to "Frieds" after ajax submit how i can do this
    – Anaz
    Feb 25, 2015 at 6:26
  • hey! just set some value in your $form_state in the submit handler. in your form building function add sth like if (isset($form_state['storage']['friend_added']) && $form_state['storage']['friend_added']) { // button "friends" } else { // button "add friend" } dont forget to rebuild the form in this case and it should work. for a detailled answer better ask a new question and close this one ;) cheers
    – schlicki
    Feb 25, 2015 at 6:36
  • <?php function customajax_form_custom_submit($form, &$form_state) { // do your db query drupal_set_message(t('Something has been done!')); // your message $form_state['rebuild'] = true; // rebuild the form. not sure if this is required now. } function customajax_form_callback($form, &$form_state) { return $form['submit']; } ?> how to rename the button and change the function here?
    – Anaz
    Feb 25, 2015 at 6:40
  • you need to add your button in the form function and you need to check which button to display in the form function. just set a flag in the submit function which one to display.
    – schlicki
    Feb 25, 2015 at 6:52
0

If you have Rules module installed, then the Rules Link module has the ability to create an ajax button and you can configure in the attached rule where the data will be stored.

Another option is the Flag module, which allows more configuration.

0

Your Answer

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

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