我尝试在db
中插入值$cn = mysqli_connect("localhost","root","","testajax") or die("Opps some thing went wrong");
if(isset($_POST['bottonsave']))
{
$sql = "insert into tableajax (studentname,gender,phone) values ('{$_POST[studentname]}','{$_POST[gender]}','{$_POST[phone]}')";
$result = mysqli_query($cn,$sql);
if($result == false)
{
echo 'error - ';
echo mysqli_error($cn);
}
else
{
echo "all good'";
}
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id ="testforajax">
<tr>
<td>StudentName </td>
<td>:</td>
<td><input type ="text" id ="studentname" name ="studentname" >
</td>
</tr>
<tr>
<td>gender </td>
<td>:</td>
<td><input type ="text" id ="gender" name ="gender" >
</td>
</tr>
<tr>
<td>Phone </td>
<td>:</td>
<td><input type ="text" id ="Phone" name ="Phone" >
</td>
</tr>
<input type="button" value ="save" id ="save">
</div>
<script type="text/javascript">
$(function(){
// create ajax insert record
$("#save").click(function(){
var namestudent = $('#studentname').val();
var genderstudent = $('#gender').val();
var phonestudent = $('#phone').val();
$.ajax({
url : "index.php",
type: "POST",
async: false,
data: {
buttonsave : 1,
studentname : namestudent,
gender : genderstudent,
phone : phonestudent
},
success: function(result)
{
alert("success");
}
});
});
});
</script>