防止刷新浏览器时重新提交提交 [英] Preventing a submission from being re-submitted upon refreshing a browser

查看:242
本文介绍了防止刷新浏览器时重新提交提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码非常有用.在名为Submit.php的文件上,用户可以通过表单输入提交内容.表单转到Submit2.php,其中一些代码将提交的内容插入到MySQL数据库中.到目前为止一切顺利.

The code below all works great. On a file called submit.php, a user can enter a submission through a form. The form goes to submit2.php, where some code inserts the submission into a MySQL database. So far so good.

这是问题所在:用户登录到Submit2.php后,如果用户刷新浏览器,则会出现确认表单重新提交"弹出框.然后,如果用户在该弹出窗口中单击继续",则提交将重新提交到MySQL数据库.

Here's the problem: once the user has landed on submit2.php, if the user refreshes the browser, a "Confirm Form Resubmission" pop-up box appears. Then, if the user hits "Continue" on that pop-up, the submission will be re-submitted to the MySQL database.

如何使其在submit2.php上执行以下操作:

How can make it do the following on submit2.php:

  1. 如果刷新浏览器,则不会出现弹出窗口.

  1. The pop-up will not appear if the browser is refreshed.

提交内容不会重新提交到数据库.

The submission will not be re-submitted to the database.

预先感谢

约翰

在submit.php上:

On submit.php:

echo '<form action="http://www.domain.com/sample/submit2.php" method="post"> 
    <input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">  

    <div class="submissiontitle"><label for="title">Story Title:</label></div> 
    <div class="submissionfield"><input name="title" type="title" id="title" maxlength="1000"></div>  

    <div class="urltitle"><label for="url">Link:</label></div> 
    <div class="urlfield"><input name="url" type="text" id="url" maxlength="500"></div>

    <div class="submissionbutton"><input name="submit" type="submit" value="Submit"></div> 
</form>
';

在submit2.php上:

On submit2.php:

if (isLoggedIn() == true)
{

$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$cleanurl = str_replace($remove_array, "", $_POST['url']);
$cleanurl = strtolower($cleanurl);
$cleanurl = preg_replace('/\/$/','',$cleanurl);
$cleanurl = stripslashes($cleanurl);

$title = $_POST['title'];
$uid = $_POST['uid'];
$title = mysql_real_escape_string($title);
$title = stripslashes($title);
$slug = str_replace(' ', '-', $title);

echo '-'.$site1.'-';

$cleanurl = mysql_real_escape_string(trim($cleanurl));

$site1 = 'http://' . $cleanurl;

$displayurl = parse_url($site1, PHP_URL_HOST);

function isURL($url1 = NULL) {
        if($url1==NULL) return false;

        $protocol = '(http://|https://)';
        $allowed = '[-a-z0-9]{1,63}';

        $regex = "^". $protocol . // must include the protocol
                         '(' . $allowed . '\.)'. // 1 or several sub domains with a max of 63 chars
                         '[a-z]' . '{2,6}'; // followed by a TLD
        if(eregi($regex, $url1)==true) return true;
        else return false;
}



if(isURL($site1)==true)
    mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL)");
else
    echo "<p class=\"topicu\">Not a valid URL.</p>\n";

} else {
    // user is not loggedin
    show_loginform();
}

推荐答案

用户 POST/重定向/GET 模型.

http://www.andypemberton.com/engineering/the-post-redirect-get-pattern/

这篇关于防止刷新浏览器时重新提交提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆