Fighting to create an ajax request. I've been reading couple of articles but still I can't get my plugin to send and recive ajax datas.
first init the js
function ajax_load_scripts() {
// load our jquery file that sends the $.post request
wp_enqueue_script( "ajax-calls", plugin_dir_url( __FILE__ ) . '/assets/js/ajax_calls.js', array( 'jquery' ) );
// make the ajaxurl var available to the above script
wp_localize_script( 'ajax-calls', 'the_ajax_script',
array( 'ajaxurl' => plugin_dir_url('my_plugin.php') ,
'_ajax_nonce' => wp_create_nonce( 'my_ajax_nonce' )
) );
}
add_action('wp_print_scripts', 'ajax_load_scripts');
than trying to catch posted data in my plugins entry file
function tipps_processing_function(){
check_ajax_referer('my_ajax_nonce');
if ($_POST['results']){
$token = '393a9276ae329e00b3739d2e76e52f3b';
$tips = json_encode($_POST['results']);
$save = new Tipspiel();
$save->save_tips($token, $tips);
var_dump($_POST['results']);
return 'OK';
}
//do stuff here
}
add_action('wp_ajax_nopriv_add_tipps', 'text_ajax_process_request');
my js
$j("form#tiepspiel").submit(function(e) {
e.preventDefault(); // this disables the submit button so the user stays on the page
var str = $j(this).serialize();
//alert(decodeURIComponent(str));
if ($j('ul#sortables li').size() > 10){
$j( "#dialog-modal" ).dialog({
height: 140,
modal: true
});
return;
}
var data = {
action: 'add_tipps',
tips: str,
_ajax_nonce: the_ajax_script._ajax_nonce
};
$j.post(the_ajax_script.ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
//$j.cookie("tipspiel", "competitor", { expires: 1 });
$j('form#tiepspiel').fadeOut('slow', function() {
// Animation complete.
});
});
});
but I don't see my varibales dumped
$jyour alias forjQuery? – chrisguitarguy Dec 2 '12 at 21:04