简体   繁体   English

如何使表单仅在单击按钮时提交而不在刷新时提交?

[英]How to make a form submit only on button click not on refresh?

I have a form where i should make the form submit(request to a service) only if the form field is changed, If the field is not changed when the user refresh the page it should not submit the form again. 我有一个表单,仅当表单字段更改时,我才应使表单提交(对服务的请求);如果用户刷新页面时字段未更改,则不应再次提交表单。

OBJECTIVE is to reduce the number of form submission as possible, So if the user has the same entered data and he refresh, we shouldn't waste the previous returned datas and try to give new request and show the same datas. 目的是尽可能减少表单提交的数量,因此,如果用户输入的数据相同并且他刷新,我们不应浪费先前返回的数据并尝试提出新的请求并显示相同的数据。

Instead it should be submitted only if the TEXT(searchdirectory) is changed. 相反,仅当TEXT(searchdirectory)更改时才应提交。 So there won't be repeating request for same datas. 因此,不会重复请求相同的数据。

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">            
        <label>TEXT :</label>
        <input type="text" id="search_dir" name="searchdirectory"
            value="<?php
                    if (isset($_POST['searchdirectory']))
                    {
                        echo($_POST['searchdirectory']); 
                    }
                   ?>"
        />            
        <input type="submit" value="Search" name="submit" id="searchB"/>
        <?php            
            if (isset($_POST['submit'])) 
            {
             //functions  to show results queried from searchdirectory
            }
        ?>
    </form>

I guess this would be to deal with refresh behaviour of browsers, Is there a way to stop the form submission if user refresh the page. 我想这将是处理浏览器的刷新行为,如果用户刷新页面,有没有办法停止提交表单。 It should show the same datas. 它应该显示相同的数据。

Use redirect when form data was sent at first. 第一次发送表单数据时,请使用重定向。

<?php            
    if (isset($_POST['submit'])) 
    {
        //do function
        header("Location: http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
    }
?>

Where "http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] 其中"http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] "http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] "http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] - is a link to which the user will be redirected after form submit. "http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] -是一个链接,表单提交后用户将被重定向到该链接。

There is two ways to go. 有两种方法。 But I try to address your objective, and not your specific question :) 但是我尝试解决您的目标,而不是您的特定问题:)

  1. You can forward the browser to a new url after form submit. 提交表单后,您可以将浏览器转发到新的URL。 This is a very normal procedure. 这是非常正常的过程。 So that when your form submits the browser redirects immediately to a normal GET URL. 这样,当您提交表单时,浏览器会立即重定向到普通的GET URL。 Now if the user hit refresh the form is not submitted again. 现在,如果用户单击刷新,则不会再次提交表单。

2 The other approach that you should use if the form is login dialog etc. Add a one time session key to a forms hidden field, and store the same key in on the server. 2如果表单是登录对话框等,则应使用的另一种方法。将一次性会话密钥添加到表单隐藏字段中,并将相同的密钥存储在服务器上。 Now if the user submits the form check that the keys match. 现在,如果用户提交表单,请检查键是否匹配。 If they do accept the submit, and clear the key. 如果他们确实接受提交,请清除密钥。 If the user now hit F5 again, and you've already removed the key, simply reject the values submitted. 如果用户现在再次按下F5,并且您已经删除了键,只需拒绝提交的值即可。

为什么在提交后不清除表单,或者为每个提交请求添加会话

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

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