【发布时间】:2021-10-20 13:13:22
【问题描述】:
所以我有一个网站,它有三个表单用于将数据添加到我的数据库中,每次我提交表单时页面都会刷新,我听说我应该使用 AJAX 来完成,而不需要页面自行刷新。谁能指出我应该使用什么?
【问题讨论】:
-
是的,你应该使用 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
}
?>
【讨论】: