1

Ok, i've been stuck on this for some time...

I have my own module where I alter the form to add my own button that returns a message from the server via AJAX. I'm stuck on the ajax part. When I click on the button, I see the url change to ../myforms/ajax But I get the same page returned as output instead of my ajax message

Here are my code snippets.

I click on the button that calls my getAjaxReply function:


function getAjaxReply(context, settings)
{
console.log("this works");
$url = this.href;
    jQuery.ajax
    (
        {
            type:'POST',
            url: '/myforms/ajax',
            dataType: 'json',
            success:voteSaved,
            data:'getAjaxReply=testingAjax',
        }
    )
    return false;
}

In my php file, I have

 
function myforms_menu()
{
    $items['myforms/ajax'] = array(
    'title' => t('myforms AJAX'),
    'type' => MENU_CALLBACK,
    'page callback' => 'myforms_ajaxCallback',
    'access arguments' => array('access content'),
     );

 return $items;

}

//the php function function myforms_ajaxCallback() { if($_POST['getAjaxReply'] == 'testingAjax') { return drupal_json( array( 'answer' => 'success' ) ); } return false; }

4
  • How are you implementing myforms_ajaxCallback function? Commented Jun 19, 2012 at 12:42
  • OK updated the question with the myforms_ajaxCallback function
    – airnet
    Commented Jun 20, 2012 at 11:26
  • Just as an FYI, Drupal doesn't like camel case in PHP.
    – Chapabu
    Commented Jun 20, 2012 at 11:44
  • Have you cleared the menu cache?
    – Chapabu
    Commented Jun 20, 2012 at 11:45

2 Answers 2

1

you also may consider using Drupal Ajax form API. This way you don't have to write any JS code or hook_menu entries. You basically alter your form to take user input into account. Just take a look at the examples: http://drupal.org/node/752056

I recently ajaxified form by hand and after that reworked it to make use of api. And didn't regret ;)

0

Do not return when calling drupal_json()

function myforms_ajaxCallback(){
    if($_POST['getAjaxReply'] == 'testingAjax'){
      drupal_json(array('answer' => 'success'));
    }else{
      drupal_json(array('answer' => 'failure'));
    }
}

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.