javascript - 单击提交按钮后页面重新加载
问题描述
$(document).ready(function() {
$("submit").click(function(event) {
event.preventDefault();
});
});
function norefresh() {
return false;
}
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
<div class="form-group">
<div class="row">
<div class="col-xs-6 col-md-6 form-group">
<label for="lname1">NAME OF THE LANDLORD</label>
<input type="text" class="form-control" id="lname" name="lname"></div>
<div class="col-xs-6 col-md-6 form-group">
<label for="lpan1">PAN OF LANDLORD</label>
<input type="text" class="form-control" id="lpan" name="lpan">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-6 col-md-6 form-group">
<label for="ladd1">ADDRESS OF LANDLORD</label>
<input type="text" class="form-control" id="ladd" name="ladd">
</div>
<div class="col-xs-6 col-md-6 form-group">
<label for="lacc1">ACCOMODATION ADDRESS</label>
<input type="text" class="form-control" id="lacc" name="lacc">
</div>
</div>
</div>
<input type="submit" class="btn btn-primary pull-right" name="submit" value="Add New" onClick="getintotab();resetform();return false;" />
<br> <br>
</form>
</div>
<?php
if(isset($_POST['submit'])){
$sql1 = $db->query("INSERT INTO Landlord(name, landlord_pan, landlord_address, acc_address) VALUES ('".$_POST["lname"]."','".$_POST["lpan"]."','".$_POST["ladd"]."','".$_POST["lacc"]."')");
if($sql1 == TRUE){
echo "";
}
else{
echo "Error: " . $sql1 . "<br>" . mysqli_error($db);
}
}
?>
单击提交按钮时的 php 脚本将表单数据发送到 mysql 数据库,该getintotab()
函数将表单值存储在表中的同一页面上,resetform()
重置表单,但单击提交按钮时,页面会重新加载。
我已经尝试过以下方法:
norefresh()
preventdefault()
- 将输入类型设置为按钮而不是提交
仍然没有运气
解决方案
您需要使用 event.preventDefault() 但您的选择器“提交”不会匹配任何内容。
将标识符添加到您的提交按钮:
<input type="submit" id="submit" ... >
然后将其用作您的选择器:
$("#submit").click(function(event) {
event.preventDefault();
# You should add the other function calls here
# instead of having function calls in two separate places.
});
或者在您的表单中添加一个标识符:
<form id="form" ... >
然后为提交操作添加一个监听器(我推荐的方式):
$("#form").submit(function(event) {
event.preventDefault();
# You should add the other function calls here
# instead of having function calls in two separate places.
});
推荐阅读
- azure-sqldw - 为什么 sys.schemas 和 INFORMATION_SCHEMA.SCHEMATA 返回不同的结果?
- agens-graph - 如何删除 AgnsGraph 上的所有图表?
- c++ - 为什么不能在Visual C++中动态分配堆栈内存?但 gcc 可以做到
- django - 使用方括号问题中的 result.field_name 在模板中显示 haystack 搜索结果
- c# - 通过 IHttpClientFactory 忽略 SSL 连接错误
- c - 马里奥问题集的金字塔右对齐有困难
- sql-server - SQL Server 与 Docker 和实体框架的连接
- react-native - 如何在 react-native-simple-dialogs 插件上更改正面或负面按钮上的字体系列
- c# - 强制 .Net 程序使用 '.' 作为十进制符号
- android - android应用程序类启动时运行flutter代码