Assistance Learning AJAX
-
I am new to wordpress development and am attempting to understand using AJAX. My understanding is that I can use AJAX to send variable information on the client side (form input fields, etc.) to the server side where they can be extracted via $_POST and used in server side functions. Hopefully that ‘big picture’ is correct.
I’ve created a simple plugin to learn how to accomplish this. The plugin creates a simple form with one checkbox and one input field. I want to take the value of the input field, send it to the server via AJAX and then manipulate it via php on the server and echo the result back to the client.I believe I need to add something similar to the following in the JS file to send the input field value to the server, but I am really not sure what some of the parameters are supposed to be.
var $jq = jQuery.noConflict(); var g_name = document.getElementById("ajax_guest_name").value; // so far so good! $jq.ajax({ url : ajax_test.ajax_url, // I'm not sure what exactly belongs here type : 'post', data : { ajax_guest_name : 'g_name', // I believe this fills the ajax data array with the input field value }, success : function() { alert("AJAX Success???"); // I'm not sure what exactly belongs here } }); // End of AJAX function
Then I believe I need to add something similar to the following in the plugin php file, but am very unclear as to what is required:
add_action( 'wp_ajax_my_ajax_test', 'my_ajax_callback' ); function my_ajax_callback() { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { die(); } else { exit(); } }
Any help would be appreciated.
Thanks
- The topic ‘Assistance Learning AJAX’ is closed to new replies.