0

I am trying to assign value I receive from the php file to the jquery variable. The php file runs a loop and echos out a value. I am trying then to store the value in the jquery variable but I am getting NaN result back. How can I assign the value echoed from the php file into the variable in the jquery function?

    var increment;

    event.preventDefault();
    $.ajax({
            type: 'POST',
            url: 'increment.php',
            data: $(this).serialize(),
            dataType: 'text',
            success: function (data) {
                    //console.log(data);
                    data = increment;
            }
    });
1

2 Answers 2

1

You should be using

increment = data;

instead of

data = increment;

Also one more thing to note here is the request nature. Since the ajax request is asynchronous accessing the variable outside might show unexpected result. You have to access the variable once the ajax request is successful (inside success callback).

Sign up to request clarification or add additional context in comments.

2 Comments

Oh I see now. I may have to assign the result to an input field and then assign the value of that input field to var increment
You don't have to create an additional input field for this. Just make sure that you are assigning the response only when the request is complete.
0

do like so,you should assign data recieved to increment var:

    var increment;

event.preventDefault();
$.ajax({
        type: 'POST',
        url: 'increment.php',
        data: $(this).serialize(),
        dataType: 'text',
        success: function (data) {
                //console.log(data);
                increment = data ;
        }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.