Insert query using ajax drupal
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:

&lt;?php

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

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

function MY_MODULE_form_custom_submit($form, &amp;$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, &amp;$form_state) {
  return $form['submit'];
}

?&gt;


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

cheers,
j