After a hole day finding solution for the issue, i didn't get what is exactly i am making the mistake. Please give your valuable answer for my issue and humble request not to block if you can't answer.
so i am trying Ajax Call in WordPress and getting issue "Fatal error: Call to undefined function add_action() in C:\xampp\htdocs\brandcart\wp-content\themes\twentyten\functions.php on line 51"
WordPress : 4.1.1 Theme : Twenty-ten
Here is my code:
in Twenty-ten page.php
<input type="hidden" name="GreetingAll" id="GreetingAll" value="Hello Everyone!" />
<input type="submit" id="PleasePushMe" />
<div id="test-div1">
</div>
in Twenty-ten functions.php
add_action( 'init', 'add_myjavascript' );
function add_myjavascript(){
wp_enqueue_script( 'ajax-implementation.js', get_bloginfo('template_directory') . '/script/ajax-implementation.js', array( 'jquery' ) );
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction22' );
add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction22' );
function MyAjaxFunction22(){
//get the data from ajax() call
$GreetingAll = $_POST['GreetingAll '];
$results = "<h2>".$GreetingAll."</h2>";
// Return the String
die($results);
}
and my ajax-implementation.js (which is in script directory in the theme)
jQuery(document).ready(function() {
var GreetingAll = jQuery("#GreetingAll").val();
jQuery("#PleasePushMe").click(function(){ jQuery.ajax({
type: 'POST',
url: 'http://localhost/brandcart/wp-content/themes/twentyten/functions.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);
}
});
});
});