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:
- Replace normal submit handler by custom submit handler
- Add an AJAX callback and wrapper to your submit button
- Do your database queries in the submit handler, force rebuilding the form and define the message.
- 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