如何prevent从刷新页面上的提交按钮点击按钮、页面、prevent

2023-09-10 17:08:02 作者:12.矢遇

我有一个输入和一个表单提交按钮。

i have form with one input and one submit button.

<form method='POST' action='' enctype='multipart/form-data' id="form_search">
<input type='hidden' name="action" id="form_1" value='1' />
</span><input id="query" type="text" name="mol" value="">
<input type='submit' value='Search' name="Search" id="Search" />

在表单提交表单输入的数据保存到PHP低于code

on form submission form input data goes to php below code

if (isset($_POST['Search'])) {
$_SESSION["query"] = $_POST["mol"];
$_SESSION["action"] = $_POST["action"];
}

我想避免对表单提交页面刷新。我想ËpreventDefault(),并返回false。 在我的Java脚本方法,但不工作(这个方法帮助我从一个页面刷新,但不会让我将数据发送到PHP code)

i want to avoid page refresh on form submission. i tried e.preventDefault() and return false; methods in my java script but not working(this methods helping me from page refresh but does not allowing me to send data to php code)

请帮我出这个问题,请建议工作阿贾克斯code这个问题。

please help me out of this problem, please suggest working ajax code for this problem.

推荐答案

页面刷新会删除你previous数据,以便保留它,你可以使用$。员额()或$。阿贾克斯()

Page refresh will delete you previous data so to reserve it you can use $.post() or $.ajax()

您可以prevent页面清爽在事件处理函数中添加这两种情况之一

You can prevent page refreshing by adding one of these two things in event handler function

纯JS

 return false;

有关jQuery的你可以用

for jquery you can use

e.preventDefault(); // e is passed to handler

您完成code会像

使用的 $。员额()在JS

function checkfunction(obj){
$.post("your_url.php",$(obj).serialize(),function(data){
 alert("success");
 });
 return false;
 }

HTML

html

<input type='submit' onclick="return checkfunction(this)" />

或onsubmit的相同效果

or same effect with onsubmit

<form  onsubmit="return checkfunction(this)" method="post">