I’ve got some problems with passing a variable through ajax from my javascript to php in the next file.
This is code I used to send the data of the room_id (if i go to console log it shows that ajax sends it well, but it keeps showing me the error
Error: Data not found in POST
What could be the problem here? Filenames and variables are okay. Thanks for help in advance.
$.ajax({
type: 'POST',
url: 'functions.php',
data: { room_id: selectedRoom },
success: function(response) {
console.log(response);
},
error: function(error) {
console.error(error);
}
});
});
});
and this code to recive that in PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['room_id'])) {
$room_id = $_POST['room_id'];
// Dodaj debug statement
echo "Received room_id: " . $room_id;
$response = $room_id;
echo $response;
} else {
echo "Error: key not included 'room_id' in POST data.";
}
} else {
echo "Error: Data not found in POST.";
}
I’ve tried to get the variable from js to php to use it variable in SELECT query.