I have a site on which I have two values of which I want to submit to a PHP script. I would like this to happen whenever a specific action is done by the user. It has to be "silent", as in it has to just run in the background, hence the reason I want to submit it from AJAX to PHP. I have a code, which seems right, but somehow doesn't work. I have used the line
success: function () {
alert('data was submitted');
}
To check whether it even submits the data and it doesn't reach this, as the alert isn't showing up.
HTML / Ajax
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function ch() {
var customID = document.getElementById('hiddenCustomerValue').innerHTML;
var customEmail = document.getElementById('customerEmail').innerHTML;
var dataString = 'customID='+customID+'&customEmail='+customEmail;
$.ajax({
type:"POST",
url: "testSilence.php",
data:dataString,
cache: false,
success: function () {
alert('data was submitted');
}
});
return false;
}
</script>
</head>
<body>
<p id="hiddenCustomerValue" style="color:white;">customerKeyID</p>
<p id="customerEmail" style="color:white;">test</p>
<button id="send_btn" onclick="ch()">Submit</button>
</body>
PHP
<?php
$customerID = $_POST['customID'];
$customerEmail = $_POST['customEmail'];
?>
successhandler does not run then you are not getting a 200 response from the server. Use the network tab of the console to see the response of the request along with the status code and exact response text. Please let us know what that is.