简体   繁体   English

如何防止在提交按钮单击时刷新页面

[英]How to prevent from page refresh on submit button click

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" />

on form submission form input data goes to php below code在表单提交表单输入数据转到 php 下面的代码

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

i want to avoid page refresh on form submission.我想避免在表单提交时刷新页面。 i tried e.preventDefault() and return false;我试过 e.preventDefault() 并返回 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)我的java脚本中的方法但不起作用(这些方法帮助我刷新页面但不允许我将数据发送到php代码)

please help me out of this problem, please suggest working ajax code for this problem.请帮我解决这个问题,请建议使用 ajax 代码来解决这个问题。

Page refresh will delete you previous data so to reserve it you can use $.post() or $.ajax()页面刷新将删除您以前的数据,因此您可以使用 $.post() 或 $.ajax() 保留它

You can prevent page refreshing by adding one of these two things in event handler function您可以通过在事件处理函数中添加这两件事之一来防止页面刷新

for pure js对于纯js

 return false;

for jquery you can use对于 jquery,您可以使用

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

Your complete code will be something like您的完整代码将类似于

using $.post() in js在 js 中使用$.post()

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

html html

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

or same effect with onsubmit或与 onsubmit 相同的效果

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

Without ajax you can simply add the checked attribute in PHP.如果没有 ajax,您可以简单地在 PHP 中添加 checked 属性。 So for example if your radio group has the name radio and one has value a , the other b :因此,例如,如果您的无线电组名为radio并且一个值为a ,另一个为b

<?php
$a_checked = $_POST['radio'] === 'a';
$b_checked = $_POST['radio'] === 'b';
?>

<input type="radio" name="radio" value="a"<?=($a_checked ? ' checked' : '')?>></input>
<input type="radio" name="radio" value="b"<?=($b_checked ? ' checked' : '')?>></input>

So when a user submits the form and you display it again, it will be like the user submitted it even the page refreshes.因此,当用户提交表单并再次显示它时,即使页面刷新,它也会像用户提交它一样。

<input type="radio" name="rbutton" id="r1">R1
<input type="radio" name="rbutton" id="r2">R2
<input type="button" id="go" value="SUBMIT" />
<div id="result"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $('#go').click(function(){
        var val1 = $('input:radio[name=rbutton]:checked').val();
        var datastring = "partialName="+val1;
        $.ajax({
            url: "search.php",
            type: "POST",
            data: datastring,
            success: function(data)
            {
                $("#result").html(data);
            }
        });
    });
});
</script>

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

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