After I've submitted a form via ajax, I want to show a message that says that the form was submitted successfully. Preferably showing the data that was submitted. I've been trying to use ajax_command_prepend, or ajax_command_append to add the message to the form. Unfortunately, I keep getting an error. The form is processed correctly and the data is added to my table, but the message is not showing up. I get this error: http://develzone.aetwirk.com/test/ajaxerror.jpg

My form code:

/**
 * Add a term
 */
function markit_form_term_add()
{
    $sets = markit_get_marksets();
    //drupal_set_message(var_export($sets));
    $form = array();

    $form['add'] = array(
        '#type' => 'fieldset',
        '#title' => t('Add new Term'),
        '#prefix' => '<div id="add_term_form">',
        '#suffix' => '</div>',
    );
    $form['add']['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Term name'),
        '#size' => 15,
    );
    //select the set you want to add the term to

    $setNames = array();
    foreach($sets as $row)
    {
        $id = $row['set_id'];
        $setNames[$id] = t($row['set_name']);
    }
    $form['add']['sets']['set_names'] = array(
        '#type' => 'select',
        '#title' => t('Select a set.'),
        '#options' => $setNames,
        '#description' => t('Select from the list of sets.'),
        );

    $form['add']['submit'] = array(
        '#type'  => 'submit',
        '#value' => t('Add'),
        '#ajax' => array(
            'callback' => 'markit_ajax_terms_add_callback',
        ),
    );
    return $form;
}

function markit_ajax_terms_add_callback($form, $form_state)
{
    
    $entry = array(
       'term_name' => $form_state['values']['name'],
        'set_id' => $form_state['values']['set_names'],
        );
    $return = markit_form_term_add_insert($entry);
    if($return)
    {
        $message = "Created entry {$form_state['values']['name']}";
        ajax_command_append('#add_term_form',$message);//'<h2>It worked!</h2>');
        //drupal_set_message(t("Created entry @entry", array('@entry' => print_r($entry, TRUE))));
    }
    else
    {
        print t('Did not insert.');
    }
}

So, how do I fix it?

Comments

Anonymous’s picture

'#ajax' => array(
    'callback' => 'markit_ajax_terms_add_callback',
)

Hi Jerrac, I dont see #ajax property in the documentation, this link could help you
----
oops, D7 does support this :D
Maybe you should 'return' rather than 'print'

jerrac’s picture

Yeah, I did tag my post with D7. :D

The print command isn't ever being called. The markit_form_term_add_insert($entry) works fine. So it $return is evaluated as true.

I did try commenting out the print command and running it. It still gave me the same error.

Doesn't Drupal have a good way to set messages via ajax? I haven't seen anything that is easy in the docs or th example modules.

Another way I've thought of is to create an empty form element and then replace it with the message. But I'm not sure what kind of form element would look decent when it's empty. *goes off to try anyway*

jerrac’s picture

Right, I'm trying a different method than before. Pretty much the way the ajax example module works. But I still get the same kind of error. Since the example module works fine, I know it's something I'm doing wrong. *sigh*

So, I added a new piece to the form to act as a message holder:

$form['message'] = array(
        '#type' => '' . !empty($form_state['values']['name']) ? 'textfield' :  'hidden',
        '#description' => '' . !empty($form_state['values']['name']) ? 'Added term: ' . $form_state['values']['name'] :  'Message goes here.',
        '#prefix' => '<div id="form_message">',
        '#suffix' => '</div>',
    );

And I changed the ajax callback function to return the form, like in the example module.

function markit_ajax_terms_add_callback($form, $form_state)
{
    
    $entry = array(
       'term_name' => $form_state['values']['name'],
        'set_id' => $form_state['values']['set_names'],
        );

    if(markit_form_term_add_insert($entry))
    {
        return $form['message'];//return the form the same way the example module does.
    }
    else
    {
        //nothing
    }
}

My second error message: http://develzone.aetwirk.com/test/ajaxerror2.jpg

For reference the example modules: http://drupal.org/project/examples

jerrac’s picture

I should have mentioned that I changed this:

$form['add']['submit'] = array(
        '#type'  => 'submit',
        '#value' => t('Add'),
        '#ajax' => array(
            'callback' => 'markit_ajax_terms_add_callback',
        ),
    );

To this:

$form['add']['submit'] = array(
        '#type'  => 'button',
        '#value' => t('Add'),
        '#ajax' => array(
            'callback' => 'markit_ajax_terms_add_callback',
            'wrapper' => 'form_message',
            'method' => 'replace',
        ),
    );
jerrac’s picture

Since it's been a day with no response, I thought I'd note that I am still banging my head against this problem. Can't even think of new ways to attack it. Any help would be much appreciated.

nevets_cck_views’s picture

Hi,
Your callback function is not coded correctly. The Drupal 7 examples detail the proper code.

In hook_form(), you need to define a "wrapper" element that is the target of your ajax callback response. A div element is used. You have to set the "id" attribute in the wrapper element and the reference that id in the button definition (or which ever control initiates the ajax event) ,

In the server side call back you set the form element and then return it (you are doing any of that).

http://drupal.org/project/examples

Drupal is open source. That means you can peruse the source code to see how other part of the system are performing your task. Did you look at the source?

jerrac’s picture

Erm. I am doing exactly what you said. I also am using the example module as a reference, as I mentioned above.

I assume you saw something specific in my callback function that is wrong. What did you see? I modeled it after the callback functions in the example module.

And yes, I know Drupal is open source... And yes, I am looking at the code. I really don't get why you made that comment....

"(you are doing any of that)."

What do you mean by that?

nevets_cck_views’s picture

Hi,
In you server side callback, you aren't returning anything. You need to either return a form field or an array of ajax commands. All the examples have a return statement, yours doesn't.

I like to use return statements, specially when the application code requires it. Return statements allow you to return data required by the application to complete processing successfully. Here is a an example from the Drupal 7 Ajax examples:

function ajax_example_simplest_callback($form, $form_state) {
  // The form has already been submitted and updated. We can return the replaced
  // item as it is.
  return $form['replace_textfield'];
}

See :) They use a return statement. They returned a form field to make the application process correctly.

jerrac’s picture

Erm, actually, I am. If you look a few posts above, I mention that I changed the way I was trying to return the message. Look at my comment: http://drupal.org/node/872088#comment-3283588

And it is definitely reaching that return statement since my data is being inserted into the database.

jerrac’s picture

Right. In markit_form_term_add_insert (which I didn't post here), I had some code that I had been using to figure out some previous bugs. Deleting that fixed the problem. The debug code was the bug.... *bangs head against wall*

Specifically, I was using drupal_set_message to view the contents of a variable and make sure they were correct. But when I started using ajax, that drupal_set_message wasn't being called correctly anymore. It's supposed be called after a form is submitted, or, apparently, returned by the ajax callback. If it's just called like you would outside of ajax, it gives those errors.

Anyway, hope that this helps someone when they are searching for similar errors.

Basically, go through every single function that is being called. Even the ones that you know are not affecting anything. Somewhere in there is some code that isn't being called the way it's supposed to.