I have Ajax POST request. It always return 200 OK but I am not getting what I hope. I know there is a json problem but i can't solve this problem. Here's my code :
<script type="text/javascript" src="../js/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form#formreg").submit(function(){
if (confirm("Are You Sure Want To Save ?")){
$.ajax({
url: "save.php",
type:"post",
contentType: 'application/json; charset=utf-8',
data:$( ":input" ).serialize(),
dataType: "json",
success:function(response){
if(response.status == 1)
{
alert("Save OK !");
}
else
{
alert("Fail To Save!");
}
},
error: function(xhr){
alert("An error occurred: " + xhr.status + " " + xhr.statusText);
}
});
}
return false;
});
});
</script>
<form method="post" name="formreg" action="" id="formreg">
<table>
<tr>
<td>Name</td>
<td><input type="text" id="name" name="name" required="required" size="50" maxlength="50" /></td>
</tr>
</table>
<input type="submit" id="submit" value="Save"/>
</form>
save.php
<?php
include_once("connect.php");
$name = $_POST['name'];
mysql_query("INSERT INTO visitor(name) VALUES('$name')") or die ("Fail To Add !");
echo '{"status":"1"}';
?>
what are the wrong code so my script doesn't work, can you help me ?