Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
//Why is that I cant retrieve the value of UserID from my JavaScript code to PHP, it says that "Undefined index: userID"

//this is my Code

//JavaScript CODE

function Print(str) {
        var userID = str;
        $.ajax({
                    type: "POST",
                    url: 'ROOMS.php',
                    data: { userID : userID },
                    success: function(data)
                    {
                        alert("success!");
                    }
                });
    }


//PHP CODE

<?php

    $id = $_POST['userID'];
    echo $id;
?>
Posted
Comments
Kornfeld Eliyahu Peter 2-Sep-14 5:36am    
Try this:
data: { 'userID' : userID },
[no name] 3-Sep-14 2:58am    
still not working , but thnkx

1 solution

The variable name you are using in the javascript is same as that of the post variable name. So change any one of the variable name and try it.

JavaScript
function Print(str) {
        $.ajax({
                    type: "POST",
                    url: 'ROOMS.php',
                    data: { userID : str },
                    success: function(data)
                    {
                        alert("success!");
                    }
                });
    }
 
Share this answer
 
v2
Comments
[no name] 3-Sep-14 3:00am    
i tried your codes but still not working . and I dont know why. . . but thanks for helping
Manikandan Subbu 3-Sep-14 3:06am    
Can you put the error message here ?
[no name] 3-Sep-14 3:13am    
Undefined error variable id
Manikandan Subbu 3-Sep-14 4:43am    
Php takes a single quote string as a constant. so use double quotes to retrieve the value (like $_POST["userID"]).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900