0

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!

9
  • 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 ? Commented Apr 29, 2013 at 3:44
  • Does success get called? Commented Apr 29, 2013 at 3:45
  • I initiate and calculate the winner_id before I call the ajax function and use the winner_id inside it Commented Apr 29, 2013 at 3:45
  • what is the possible values for winner_id in $.ajax data..? Commented Apr 29, 2013 at 3:45
  • yes, success gets called Commented Apr 29, 2013 at 3:46

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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) {
...

5 Comments

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)
so if i suppose your initiated value is $winnerid just call function like ajaxCall($winnerid).
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
are you sure, your PHP code is in ajax.php at the same folder?
Yes, positive! Both, the javascript file and the php file are on my apache2
0

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();

3 Comments

Thank you for the winner_id.toString() idea. I implemented it, however I'm still unable to get it in my php script
Not necessary unless you are giving the response of ajax as json @Charlene Lauron
hmm but the function is passing json data to php, ok i'll test it.. thanks @Deepu

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.