I’m trying to use AJAX to send values to PHP file, which then updates mysql database on the server. But for some reason the values are not transferred to PHP file.
This is the JS I use:
function send_message()
{
var number = localStorage.getItem("number");
var message = prompt("Message:", "");
jQuery.ajax({ type: "POST",
url: serviceURL + "message.php",
data: 'number='+number+'&message='+message,
cache: false,
success: function(response)
{
alert("message sent");
}
});
}
And this is the message.php
<?php
include 'config.php';
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$number = $_GET['number'];
$message = $_GET['message'];
// query
$sql = "INSERT into table
SET condition=0, change=1, change_time=NOW(), recieve=999,
number=?, message=?";
$q = $conn->prepare($sql);
$q->execute(array($number, $message));
?>
Everything else is inserted to mysql except the number and message are NULL. What could be the reason for this?
You’re posting data with AJAX using POST. In order to listen for those values in PHP you need to…
Replace:
With: