When I try to submit data via AJAX to a PHP file, the submit works, and I can echo back a message from the PHP file. However, when I try to echo back the data I am submitting, or, echo back a message confirming that the data matches another variable in the PHP file(which it does), I still get a success message, but it says the data is not the same.
I am new to development and struggling a bit. Any help would be super.
The AJAX
$.ajax({
type: "POST",
url: "/check-cust.php",
data: "1234",
success: function(data) {
if (data == "") {
console.log("success, but no return");
} else {
alert(data); // show response from the php script.
}
},
error: function() {
alert("not working");
}
});
The PHP
$temp_cust_id = "1234";
$data = $_POST['data'];
if ($data == $temp_cust_id) {
echo "it works";
} else {
echo "it doesnt work";
}
Is it maybe because I am submitting a JSON array, which is different to the string variable? That's just a guess though!