Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to pass the a variable from JavaScript to PHP using AJAX, but I'm unable to do so. Whenever I try to var_dump($_POST['winner_id']) it returns NULL. I've tried to check the AJAX call with Developer Tools in Chrome and it showed winner_id:0 - which is right.

Here is my code:

JavaScript

 function ajaxCall() {

   alert("To AJAX: the winnerid is: "+winner_id);

        $.ajax
        (   {


                type: "POST",
                url: "ajax.php",
                data: {winner_id : winner_id}, 
                success: function(response)
                { alert("The winner was passed!")}
            }
        );
};
ajaxCall();

PHP Code

<?php 
session_start();

if(isset($_POST['winner_id']))

{
    $winner_id = $_POST['winner_id']."";
    var_dump($winner_id);
}

var_dump($_POST['winner_id']);

?>

If I do a var_dump($_POST) in the beginning of the PHP script then it gives me array(0) { }

I'm new to web development and have been trying to figure this out for hours now. Any hints would be much appreciated. Thanks!

share|improve this question
Uhm, if you're passing an actual zero to PHP, what are you expecting back. Did you try this with some easily identifiable strings instead ? – adeneo Apr 29 at 3:44
Does success get called? – Explosion Pills Apr 29 at 3:45
I initiate and calculate the winner_id before I call the ajax function and use the winner_id inside it – user2330619 Apr 29 at 3:45
what is the possible values for winner_id in $.ajax data..? – Dipesh Parmar Apr 29 at 3:45
yes, success gets called – user2330619 Apr 29 at 3:46
show 3 more comments

3 Answers

Where are you intializing the winner_id.Either you have to pas it as an argument or intitialize it as aglobal variable.

function ajaxCall(winner_id) {

   alert("To AJAX: the winnerid is: "+winner_id);

        $.ajax
        ({
                type: "POST",
                url: "ajax.php",
                data: {"winner_id" : winner_id}, 
                success: function(response)
                  { 
                     alert("The winner was passed!");
                  }
        });
};
ajaxCall(winner_id);
share|improve this answer
Thank you Deepu! I followed the instructions you gave me, made sure that I pass in the winner_id as the argument for the function and I can see that it passes the result, however I'm still unable to get the winner_id in my php code var_dump($_POST['winner_id'])= NULL – user2330619 Apr 29 at 4:52

Where did you initiate value to winner_id? like

function ajaxCall() {
var winner_id = '123';
...

or if you initiated winner_id before calling ajaxCall() ,you should call ajaxCall() with parameters like ajaxCall($winnerid), which $winnerid is from your PHP and then

function ajaxCall(winner_id) {
...
share|improve this answer
I tried to input the winner_id as a parameter for the ajax function and success was called, also as I went into the developer tools / network and XHR, it showed that the winner_id = 1 (which was right) – user2330619 Apr 29 at 4:04
so if i suppose your initiated value is $winnerid just call function like ajaxCall($winnerid). – Amir Apr 29 at 4:05
so what is the problem? – Amir Apr 29 at 4:12
Thank you Amir, the problem is that I'm still unable to get the winner_id in my php code: var_dump($_POST['winner_id'])= NULL – user2330619 Apr 29 at 4:53
are you sure, your PHP code is in ajax.php at the same folder? – Amir Apr 29 at 4:58
show 1 more comment

i guess you have to convert your winner_id to a string because php read zero (0) as null

function ajaxCall() {

   alert("To AJAX: the winnerid is: "+winner_id);

        $.ajax
        (   {


                type: "POST",
                url: "ajax.php",
                data: {winner_id : winner_id.toString()}, 
                success: function(response)
                { alert("The winner was passed!")},
                dataType: "json"
            }
        );
};
ajaxCall();
share|improve this answer
Thank you for the winner_id.toString() idea. I implemented it, however I'm still unable to get it in my php script – user2330619 Apr 29 at 5:09
Not necessary unless you are giving the response of ajax as json @Charlene Lauron – User016 Apr 29 at 5:16
hmm but the function is passing json data to php, ok i'll test it.. thanks @Deepu – Charlene Lauron Apr 29 at 5:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.