Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
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

1 Answer 1

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' ); 
share|improve this answer
    
Thank you , it works :) –  user33702 Jun 13 '13 at 11:20
1  
No problem, don't forget to mark the question. All the best. –  userabuser Jun 13 '13 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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