简体   繁体   English

单击提交按钮后页面被重新加载

[英]Page gets reloaded after clicking the submit button

 $(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); } } ?> 

The php script on clicking the submit button sends the form data to a mysql database and the getintotab() function stores the form value on the same page in a table resetform() resets the form but on clicking the submit button the page gets reloaded. 单击提交按钮时的php脚本会将表单数据发送到mysql数据库,而getintotab()函数将表单值存储在表的同一页中resetform()重置表单,但是单击提交按钮后,页面将重新加载。
I have already tried the following: 我已经尝试了以下方法:

  1. norefresh()
  2. preventdefault()
  3. making the input type as button instead of submit 将输入类型设为按钮而不是提交

Still no luck 还是没有运气

You need to use event.preventDefault() but your selector "submit" won't match anything. 您需要使用event.preventDefault(),但选择器“提交”将不匹配任何内容。

Either add an identifier to your submit button: 将标识符添加到提交按钮:

<input type="submit" id="submit" ... >

Then use that as your selector: 然后将其用作选择器:

  $("#submit").click(function(event) {
    event.preventDefault();
    # You should add the other function calls here 
    # instead of having function calls in two separate places.
  });

Or add an identifier to your form: 或在表单中添加一个标识符:

<form id="form" ... >

Then add a listener for the submit action (the way I'd recommend): 然后为Submit操作添加一个侦听器(我建议的方式):

  $("#form").submit(function(event) {
    event.preventDefault();
    # You should add the other function calls here 
    # instead of having function calls in two separate places.
  });

You are hitting server with a http request (form submit). 您正在通过http请求(表单提交)访问服务器。 This will result in page reload. 这将导致页面重新加载。

You should hit server by an XmlHttpRequest ie AJAX call to get/post data from/to server respectively. 您应该通过XmlHttpRequest(即AJAX调用)访问服务器,以分别从服务器获取数据/将数据发布到服务器。 This refreshes only part of page and does not reload complete page. 这仅刷新页面的一部分,而不刷新整个页面。 Hope this helps. 希望这可以帮助。

I think you can remove onClick="getintotab();resetform();return false;" 我认为您可以删除onClick="getintotab();resetform();return false;" and use all js stuff in jquery selector and it will work for you. 并在jquery选择器中使用所有js东西,它将为您工作。

you should use this trick: 您应该使用以下技巧:

<input type="submit" class="btn btn-primary pull-right" name="submit" value="Add New" />

to

<input type="button" class="btn btn-primary pull-right" name="submit" value="Add New" />

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM