I have the following jQuery:
$(document).ready(function() {
$('input[type="submit"]').click(function() {
event.preventDefault();
var email = $('.email').val();
$.ajax({
type: "POST",
url: "register_email.php",
data: JSON.stringify({ "email": email }),
dataType: "json",
contentType: "application/json",
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
});
});
The email
variable is definitely set, I can alert
it out.
However, when I get to the PHP, this is my script:
<?php
$db = new mysqli("localhost", "...", "...", "...");
if ($db->connect_error) {
echo "Could not connect to database.";
exit;
}
else {
$emerd = json_decode($_POST["email"]);
$db->query("INSERT INTO emails (email) VALUES (' " . $emerd . "')");
echo $emerd;
}
?>
It always alerts "null" back to me. Why won't it understand what I'm POSTing?