I'm sending JSON to my server via jQuery's ajax method. Here's my jquery, and im pretty sure this is fine:
function stuffs() {
this.fname = document.getElementById("fname").value;
this.lname = document.getElementById("lname").value;
this.email = document.getElementById("email").value;
}
$(function() {
function ajaxhelper(data){
//console.log(JSON.stringify(data));
//this returns "{"fname":"mike","lname":"smith","email":"[email protected]"}" which is what i expect
$.ajax({
url: 'postdb.php',
type: 'POST',
data: {data : JSON.stringify(data)},
success: function(data) {
console.log(JSON.stringify(data));
console.log("Success");
},
error: function(e) {
console.log(e);
}
});
}
$("form").on('submit', function(e){
e.preventDefault();
var data = new stuffs();
ajaxhelper(data);
//window.location = "postdb.php";
});
});
</script>
I get back an 500 server error. Here's my php code. (yes i'm only sending it an fname that i've preloading into my database, $con is valid i just didnt share the code to connect to my database)
$obj = json_decode($_POST['data']);
$sql = "SELECT * FROM testtable WHERE fname = \"$obj->{'fname'}\"";
$query = $con->query($sql);
I think my sql is incorrect due to the quotes? This is where im stuck.
$sql = "SELECT * FROM testtable WHERE fname = '{$obj->fname}'";Otherwise please check error logs.