Viewed   126 times

I'm working on an existing script at the moment which uses Ajax, something I've never worked with before. I have a variable set in my javascript file which gets its value from an input field on my page. I need to use Ajax to post this to my PHP page only I've no idea where to start,

Im not sure what code you would need to see, but My javascript/AJAX code is, the variable I need to pass is 'var credoff'

$(".getPoint").click(function () {
    var theid = $(this).attr("id");
    var onlyID = theid.split("_");
    var onlyID = onlyID[1];
    var credoff = parseInt($(this).children('input.credoff:hidden').val());

        url: 'do.php',
        type: 'POST',
        data: "userID=" + onlyID,
        success: function (data) {
            if (data != "success1" && data != "success5") {
                $("#" + theid).text(data);
            } else {

                $("#thediv_" + onlyID).fadeOut("slow");
                newbalance = parseInt($('#creditsBalance').text());

Wouldit have to be in this format?

data: "userID=" + onlyID,
"credoff=" + credoff


data: {
    userId: onlyID,
    credoff: credoff
Monday, October 10, 2022

You can include it globally with a live event:

  $(".search_button").live('click', function() {
    var searchTerm = $('#search_box').val();         
    return false;

jQuery will automatically evaluate script blocks, you cannot see the function in the HTML because it has been stripped out. However it should have already run. The problem is most likely timing. You could do something like

    $(".search_button").click(function() {
        var searchTerm = $('#search_box').val();         
        return false;
}, 500);

So that when the script is loaded it waits to be executed (hopefully giving jquery time to update the DOM with the new element).

Friday, November 4, 2022

Using Ajax in WordPress is usually done in the following manners:

1- You have to submit your ajax data to the admin-ajax.php. Sometimes you can use the global js variable ajaxurl, predefined by WordPress for that URL. But more reliably I'd use the output of admin_url( 'admin-ajax.php' ).

2- Your data object must contain an action value that have a unique string to represent this ajax submission. I'll use AJAX_ACTION here: data = { action: 'AJAX_ACTION', your_var: 'your_val' };

3- Receive the data by using hooks that contain the action name that you've specified:

add_action( 'wp_ajax_AJAX_ACTION', 'callback_function' );
add_action( 'wp_ajax_nopriv_AJAX_ACTION', 'callback_function' ); 

4- Use your callback_function to receive the data:

function callback_function() {
    // $_POST['your_var'] is accessible here

By the way, using echo inside the callback_function would result in the response being sent to the success function.

wp_ajax_nopriv_AJAX_ACTION is for submission for visitors that do not have the WordPress account or are not logged in, while wp_ajax_AJAX_ACTION is for logged in users.

Please check: WordPress Codex

Saturday, August 6, 2022

Pass the data like this to the ajax call (

data: { userID : userID }

And in your PHP do this:

    $uid = $_POST['userID'];

    // Do whatever you want with the $uid

isset() function's purpose is to check wheter the given variable exists, not to get its value.

Friday, October 28, 2022

AJAX requests call a URL (make a HTTP request), not a file, in most cases the URL is translated by the server to point at a file (or a php script in your case), but everything that happens from the HTTP request to the response that is received is up to you (on your server).

There are many PHP frameworks that map URL's to specific php functions, AJAX is just an asynchronous way to access a URL and receive a response.

Said URL CAN trigger the server to call a specific function and send back a response. But it is up to you to structure your URL's and server side code as such.

Wednesday, August 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :