function form_test_permission() {
    return array(
        'submit_form_test' => array(
            'title' => t('Submit_form_test'),
            'description' => t('Submit the form_test form'),

function form_test_menu() {
    $items = array();
    $items['production'] = array(
        'title' => 'Production',
        'type' => MENU_NORMAL_ITEM,
        'access arguments' => array('submit_form_test'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('form_test_form'),

    return $items;

function form_test_form($form, &$form_state) {
    $form['production'] = array(
        '#title' => t('production'),
        '#type' => 'hidden',
        '#value' => '1',
    $form['production1'] = array(
        '#title' => t('production1'),
        '#type' => 'hidden',

    $form['production_date'] = array(
        '#title' => t('production_date'),
        '#type' => 'hidden',
        '#value' => date('Y-m-d'),
    $form['production_time'] = array(
        '#title' => t('production_time'),
        '#type' => 'hidden',
        '#value' => date('H:i:s'),
    $form['production23'] = array(
        '#title' => t('production23'),
        '#type' => 'hidden',
        '#value' => '1',

    $form['button2'] = array(
        '#value' => 'UNDO',
        '#type' => 'submit',
        '#prefix' => '<div  class="test">',
        '#suffix' => '</div>',
        '#submit' => array('form_test_form_button2'),

    $form['button1'] = array(
    '#value' => '  ADD  ',
    '#type' => 'submit',
    '#prefix' => '<div  class="test1">',
    '#suffix' => '</div>',
    '#submit' => array('form_test_form_button1'),
    '#ajax' => array(
        'callback' => 'ajax_example_submit_driven_callback',
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'click',
        'wrapper' => 'countvalue',
    return $form;

function ajax_example_submit_driven_callback($form, $form_state) {
    $result = db_query("SELECT * FROM  form_test WHERE production_date='2014-02-10' and production_time between '01:00:00' and '02:00:00'");
    $prod_1 = $result->rowcount();
    $form['#tree'] = TRUE;
    $form_state['rebuild'] = TRUE;
    return 'count value is:' . $prod_1 . '';

function form_test_form_button1($form, &$form_state) {

    $ft_id = db_insert('form_test')
            'production' => $form_state['values']['production'],
            'production_date' => $form_state['values']['production_date'],
            'production_time' => $form_state['values']['production_time'],
            'production1' => $form_state['values']['production1'],
    $form_state['rebuild'] = true;

I have to mentioned in wrapper id in page.tpl.php file.

i want to save data using ajax. But i wrote query in AJAX callback function. AJAX Append method is worked fine. But AJAX replace function not working ? why?

  • And your code is...? Please provide us the code you're using to take a look
    – rreiss
    Commented Feb 27, 2014 at 7:26
  • This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. See Help Center.
    – Mołot
    Commented Feb 27, 2014 at 8:14
  • "Not working" is such a broad term we can't really understand and diagnose what's happening to give you the help you want. Please tell us instead of "not working": What you expect to happen, and what's happening instead. What's not working? Is the replace not happening, but record inserted in database? Is the record not being inserted into database?
    – Beebee
    Commented Feb 28, 2014 at 13:08
  • Hi... I got the solution.
    – Nandhana
    Commented Mar 3, 2014 at 4:38

1 Answer 1


Fun issue here. I pulled your code out and stuffed it into a test module and the ajax appeared to work once, eg, on first click of the ADD button, it replaced the <div id="countvalue">foo</foo> I had also stuffed into my page.tpl.php. But then each successive click put up the throbber without any further apparent change. Throwing a watchdog() into the callback showed it was indeed being called upon each click and then the voila moment:

return '<div id="countvalue">count value is:' . $prod_1 . '</div>';

is needed because it was replacing the original <div id="countvalue"> with just the count value is.... text, and then when clicked again, the DOM no longer contained a #countvalue for it to replace again :)


Your Answer

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

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