在php站点中使用Ajax



所以我有一个网站,有三个表单添加数据到我的数据库,每次我提交一个表单页面刷新,我听说我应该使用AJAX来做它没有页面刷新它自己。有人能告诉我该用什么吗?

下面是使用AJAX向数据库添加数据的演示示例。

form.php

<html>
<head>
<title>Php submit for using Ajax</title>
</head>
<body>
<form>
<input type="text" name="name"><br>
<input type="email" name="email"><br>
<input type="number" name="contact"><br>
<button type="button" id="submit">Submit</button>
</form>
</body>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function () {
$.ajax({
type: 'post',
url: 'post.php',
data: $('form').serialize(),
success: function (data) {
alert(data);
}
});
return false;
});
});
</script>
</html>

post.php

<?php
//incluce db connection file
if(isset($_POST["name"]) || isset($_POST["email"]) || isset($_POST["contact"])) {

//Data Insert Login Here
echo 'response here';//Message to be shown on success
}
?>

最新更新