Reputation: 1
I'm working with JS from the last few days, and I'm implementing chat feature. I am using AJAX post method to send and receive texts,but the problem is that the code I've written isn't working well.
JS code
function getPeople(room_code) {
console.log(room_code);
$.ajax({
url: "process_php/Getroompeople.php" ,
data: {
roomcode: room_code
} ,
cache: false ,
processData: false ,
type: "POST" ,
success: function(response) {
//implementPeople(response);
console.log(response);
}
});
setTimeout(function(){ getPeople(room_code); }, 1000);
}
PHP code
<?php
$room_code = $_POST["roomcode"];
echo $room_code;
echo "hello";
?>
The response says undefined index.
So, any help with that would be very helpful to me!
Thanks in Advance!
Upvotes: 0
Views: 146
Reputation: 943564
You are passing an object to data
.
data: { roomcode: room_code } ,
Under normal circumstances, jQuery will process this object and convert it into the application/x-www-form-urlencoded
data format.
PHP will automatically parse application/x-www-form-urlencoded
data and use it to populate $_POST
.
However, you also said:
processData: false ,
… which tells jQuery not to process it. So it will get treated as a plain string (i.e. "[object Object]"
) and sent to PHP.
Your data doesn't exist in that string, so $_POST["roomcode"]
isn't defined.
Remove processData: false ,
(or set it to true
)
Upvotes: 2