I'm trying to built a page to ask questions with a stream on top, so I need ajax to avoid the reload. It is actually simple. On top is a node with the stream and I add to blocks beneath.

  1. A table with the questions, number of votes for the questions and an "add vote" button
  2. A form to add a new node "question with 0 votes"

I have two problems.

A)When I click on the submit button of the 2. Block I add a question, I can update the table with the new question, but the add question form is not being emptied.

B)When I add a vote on the table (currently an ajax call from a checkbox) I get the database to have an increased counter and as far as I could check the callback delivers the new table, but the old one is never replaced. I thought it was the cache, but I even managed to avoid using the cached one, still no replacement. I get a few headers allready sent notices, but I don't understand why. I just want the form to be replaced by the new one.

(don't mind me getting a view instead of just a database result, it's just to built the table)

function questions_list_voting_form($form, &$form_state){
global $user;
$form['#prefix'] = '<div id="questions-list-table">';
$form['#suffix'] = '</div>';
$form['table'] = array(
        '#tree' => TRUE,
        '#theme' => 'table',
        '#header' => array(t("Title"),t("Votes"),t("Add vote")),
    $view = views_get_view('questions_view');
$view->is_cacheable = FALSE;
foreach ($view->result as $row){
    $question = array('#markup'=>$row->node_title);
    $vote = array('#markup'=>$row->field_data_field_votes_field_votes_value);
    $addvote = array(
        '#return_value' => 'add_vote'.$row->nid,
    '#ajax' => array(
      'callback' => 'ajax_addvote_callback',
      'wrapper' => 'question-list-table',
        'effect' => 'slide',
    $tab_arr = array(
            'question' => &$question,
            'vote' => &$vote,
            'addvote'.$row->nid => &$addvote
    $tab_rows = array(
            array('data' => &$question),
            array('data' => &$vote),
            array('data' => &$addvote),
            $form['table'][] = $tab_arr;
    $form['table']['#rows'][] = $tab_rows;
    $form['redirect'] = FALSE;
    $form['#rebuild'] =TRUE;
     return $form;
function questions_list_voting_form_submitx($form, $form_state){
   if(substr($form_state['triggering_element']['#value'],0,8) == 'add_vote'){
    $tmp_node = node_load(substr($form_state['triggering_element']['#value'],8,2)); //question node
function ajax_addvote_callback($form, &$form_state){
questions_list_voting_form_submitx($form, $form_state);
$form_state['no_redirect'] = TRUE; // avoid redirect?? maybe not working
$form_state['input'] = array('test'=>'etch'); //something to avoid cache 
$form = drupal_build_form('questions_list_voting_form', $form_state);
$form_html = drupal_render($form);
return $form_html; 

2 Answers 2


Your form id is 'questions-list-table' but you have 'wrapper' => 'question-list-table' (letter s is missing). You might also want to be explicit with 'method' => 'replace' since the ajax is causing problems.

You should add a '#submit' => 'submit_function' and modify your $form_state in that function instead of trying to modify it in the ajax callback.

Why do you rebuild and render the form in your ajax callback? I think you just want to return the part of the form to replace - in this case, $form itself.

  • I'm pretty sure I checked that a few times on the actual code, but you are right the typo here is the problem. I'm going to test that again. Thanks
    – reptilex
    Mar 15, 2012 at 16:54

Have a look here Remove tabledrag statuses on Ajax submit and display status message after Ajax submit I had a similar issue

Also as Joe Beuckman said you have a typo on your #ajax['wrapper'] value.

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.