Passing Javascript variable to PHP using Ajax

I am currently working on an existing script that uses Ajax, which I have never worked with. I have a variable set in my javascript file that gets its value from the input field on my page. I need to use Ajax to post this on my PHP page, I don’t know where to start,

I'm not sure which code you will need to see, but my javascript / AJAX code, 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());

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

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

Should it be in this format?

data: "userID=" + onlyID,
"credoff=" + credoff
+3
source share
2 answers
...
data: {
    userId: onlyID,
    credoff: credoff
},
...
+5
source

Or you can do this:

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

don't forget the ampersand! & Amp;

+2
source

All Articles