Javascript 如何使用ajax和php提交表单以避免页面刷新
我正在尝试用php在页面上提交表单,但我不希望页面刷新,我阅读了很多教程,但仍然无法确定是否有人应该修复我的代码 index.phpJavascript 如何使用ajax和php提交表单以避免页面刷新,javascript,php,ajax,Javascript,Php,Ajax,我正在尝试用php在页面上提交表单,但我不希望页面刷新,我阅读了很多教程,但仍然无法确定是否有人应该修复我的代码 index.php <script src="profile.js"><script> <div class="sta"> <form action="test.php" method="POST" id="sta"> <input type="text" name = "sta" id="sta" pla
<script src="profile.js"><script>
<div class="sta">
<form action="test.php" method="POST" id="sta">
<input type="text" name = "sta" id="sta" placeholder="Status">
<input id="submit" onclick="status()" type="button" value="save">
</form>
</div>
</script>
使用
onclick=“return status()”
使用
onclick=“return status()”
您在哪里使用
sta1
?-查看开发人员控制台,使用php的错误报告并检查查询中的错误(如果有)。单击Submit时使用event.preventDefault()您在使用sta1
?-查看开发人员控制台,使用php的错误报告并检查查询中是否有错误(如果有)。单击submit,使用event.preventDefault()onclick
<?php
session_start();
include 'db.php';
if(isset($_SESSION['email'])){
$eml = $_SESSION['email'];
$sql = $con->prepare("update alert_users_account SET statu=? where email_phone=?");
$sql->bind_param('ss',$_POST['sta'],$eml);
$sql->execute();
$sql->close();
}
?>
function status() {
var sta = document.getElementById("sta").value;
var dataString = 'sta1=' + sta;
if (sta == '') {
alert("Please Fill All Fields");
} else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "test.php",
data: dataString,
cache: false,
success: function(html) {
alert(html);
}
});
}
return false;
}
<input id="submit" onclick="return status()" type="button" value="save">
<form action="test.php" method="POST" onsubmit="return status()" id="sta">
<input type="text" name="sta" id="sta" placeholder="Status">
<input id="submit" type="button" value="save">
</form>