0

I have this ajax call which gets the value from a hidden input field

<p>
    <input type="hidden" name="GreetingAll" id="GreetingAll" value="Hello Everyone!" />  
    <input type="submit" id="PleasePushMe" />  
    <div id="test-div1">  
    </div>
</p>  

jQuery(document).ready(function() {  
    var GreetingAll = jQuery("#GreetingAll").val(); 
    alert(GreetingAll); 
    jQuery("#PleasePushMe").click(function(){
        jQuery.ajax({  
            type: 'POST',  
            url: 'http://www.yoursitename.com/wp-admin/admin-ajax.php',  
            data: {  
                action: 'MyAjaxFunction',  
                GreetingAll: GreetingAll,  
            },  
            success: function(data, textStatus, XMLHttpRequest){  
                jQuery("#test-div1").html('');  
                jQuery("#test-div1").append(data);  
            },  
            error: function(MLHttpRequest, textStatus, errorThrown){  
                alert(errorThrown);  
            }  
            });  
      });  
  });  

the functions fails to return or do anything

inspite of including these functions , in the function.php

function add_myjavascript(){  
    wp_enqueue_script( 'ajax-implementation.js', get_bloginfo('template_directory') . "/scripts/ajax-implementation.js", array( 'jquery' ) );  
}  
add_action( 'init', 'add_myjavascript' );
function MyAjaxFunction(){  
    //get the data from ajax() call  
    $GreetingAll = $_POST['GreetingAll '];  
    $results = "<h2>".$GreetingAll."</h2>";  
    // Return the String  
    die($results);  
}  
// creating Ajax call for WordPress  
add_action( 'wp_ajax_nopriv_ MyAjaxFunction', 'MyAjaxFunction' );  
add_action( 'wp_ajax_ MyAjaxFunction', 'MyAjaxFunction' );  

Any idea why is this code not working?

1
  • 1
    Could you the next time you post a question please take the time to format your code readable? Thank you! Btw: There're spaces in function calls, etc. Turn on WP_DEBUG and PHP error reporting and show us your error output. – kaiser Jul 6 '13 at 1:02
4

You have a space in your function name on the action call,

   add_action( 'wp_ajax_nopriv_ MyAjaxFunction', 'MyAjaxFunction' );  
   add_action( 'wp_ajax_ MyAjaxFunction', 'MyAjaxFunction' ); 

...should be,

   add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction' );  
   add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction' ); 
1
  • 1
    No problem, don't forget to mark the question. All the best. – Adam Jun 13 '13 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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