防止在刷新浏览器时重新提交提交

Joh*_*ohn 3 php mysql

下面的代码都很棒.在名为submit.php的文件上,用户可以通过表单输入提交.表单转到submit2.php,其中一些代码将提交内容插入MySQL数据库.到现在为止还挺好.

问题在于:一旦用户登陆了submit2.php,如果用户刷新浏览器,则会出现"确认表单重新提交"弹出框.然后,如果用户在弹出窗口中点击"继续",则提交将重新提交到MySQL数据库.

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

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

  2. 提交将不会重新提交到数据库.

提前致谢,

约翰

在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>
';
Run Code Online (Sandbox Code Playgroud)

在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();
}
Run Code Online (Sandbox Code Playgroud)