摆脱后退按钮确认表单重新提交页面

时间:2014-09-15 22:07:26

标签: javascript php html forms

我正在开发一个php电子邮件,它使用一个需要重定向到“谢谢”页面的表单,我无法访问该页面以添加代码。用户输入通过一些简单的preg_match函数发送,以确保它与必要的格式匹配。如果它失败了这些功能之一,则会回显javascript弹出窗口,让用户知道他们需要更改的内容。如果通过,数据将存储在SESSION变量中,并传递到将数据添加到必要的html的页面,并作为简单的html电子邮件发送。 process.php页面然后重定向到results.php,清除会话,并将用户带到“谢谢”页面。

我的问题是,当用户输入错误数据然后更正并提交时,在到达“谢谢”页面后按“后退”按钮会在返回之前获取这些“确认表单重新提交”页面之一表格。一旦它第二次点击“返回”后实际返回到表单,旧的,错误的数据将以形式代替好的东西。我想修复它,以便“确认表单重新提交”页面永远不会出现,如果用户只按一次后退按钮,则会返回一个空白表单。我的HTML:

            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
            <table id="form_table" style="280px; border:0px; padding: 0 0 5px 0; border-spacing: 0px;">
                <tr>
                    <td>
                        <span style="font-size: 12px;">*First name</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="padding: 5px; font-size: 12px;" name="firstName" type="text" size="50" value="<?php if(isset($_POST['firstName'])) { echo htmlentities($_POST['firstName']); }?>" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 7px;">
                        <span style="font-size: 12px;">*Last name</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="padding: 5px; font-size: 12px;" name="lastName" type="text" size="50" value="<?php if(isset($_POST['lastName'])) { echo htmlentities($_POST['lastName']); }?>" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 7px;">
                        <span style="font-size: 12px;">*Email</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="padding: 5px; font-size: 12px;" name="emailAddy" type="text" size="50" value="<?php if(isset($_POST['emailAddy'])) { echo htmlentities($_POST['emailAddy']); }?>" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 7px;">
                        <span style="font-size: 12px;">Phone number</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="padding: 5px; font-size: 12px;" name="phoneNumber" type="text" size="50" value="<?php if(isset($_POST['phoneNumber'])) { echo htmlentities($_POST['phoneNumber']); }?>" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 15px;">
                        <span style="font-size: 12px;">Tell us how we can help</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="padding: 5px 5px 15px 5px; font-size: 12px;" name="howHelp" type="text" size="50" value="<?php if(isset($_POST['howHelp'])) { echo htmlentities($_POST['howHelp']); } ?>" />
                    </td>
                </tr>
            </table>
            <div style="width: inherit;">
                <input type="image"  id="saveform" src="http://www.fivemm.com/client/tristar/images/consult-btn.jpg" alt="Submit Question!" style="width: 100%; height: 96px; border: none;" />
            </div>
            <?php echo $msg_to_user ?>
        </form>

处理代码,位于同一页面的底部:

<?php

if(isset($_POST['firstName'])) {

function died($error) {
    $error = 'So sorry, but it looks like there are some issues with your form.\\n\\n'.$error;
    echo '<script type="text/javascript"> alert("'.$error.'")</script>';
    die();
}

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
$msg_exp = "/^$|^[A-Za-z ?!.'-:]+$/";

if(!preg_match($string_exp,$_POST['firstName'])) {
    $error_message .= 'The first name you entered contains invalid characters or is blank.\\n\\n';
}

if(!preg_match($string_exp,$_POST['lastName']) && strlen($_POST['lastName']) > 0) {
    $error_message .= 'The last name you entered contains invalid characters.\\n\\n';
}

if(!preg_match($email_exp,$_POST['emailAddy'])) {
    $error_message .= 'The email address you entered does not appear to be valid or is blank.\\n\\n';
}

if(strlen($error_message) > 0) {
    died($error_message);
}

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

$_SESSION['firstName'] = $_POST['firstName']; // required
$_SESSION['lastName'] = $_POST['lastName']; // required
$_SESSION['emailAddy'] = $_POST['emailAddy']; // required
$_SESSION['phoneNumber'] = htmlspecialchars(clean_string($_POST['phoneNumber'])); //  required
$_SESSION['howHelp'] = clean_string($_POST['howHelp']);

echo ("<SCRIPT LANGUAGE='JavaScript'>window.location.href='process.php';</SCRIPT>");
}
?>

process.php页面:

<?php
session_start();
if(isset($_SESSION['firstName']) && isset($_SESSION['page1'])) {
    $_SESSION['page2']="2";
    $first_name = $_SESSION['firstName'];
    $last_name = $_SESSION['lastName'];
    $email_addy = $_SESSION['emailAddy'];
    $phone_number = $_SESSION['phoneNumber'];
    $how_help = $_SESSION['howHelp'];

    // EDIT THESE 2 LINES BELOW AS REQUIRED
    $email_to = 'someguy@domainname.com'; // use for testing
    $email_subject = "This Guy Wants To Contact You!"; // Enter intended message here

    $email_message = '<html>';
    $email_message .= '<head>';
    $email_message .= '</head>';
    $email_message .= '<body>';
    $email_message .= '<h3>You have a new email message!</h3>';
    $email_message .= '<table>';
    $email_message .= '<tr><td>Name</td><td>'.$first_name.' '.$last_name.'</td></tr>';
    $email_message .= '<tr><td>Email Address</td><td>'.$email_addy.'</td></tr>';
    $email_message .= '<tr><td>Phone Number</td><td>'.$phone_number.'</td></tr>';
    $email_message .= '<tr><td>How can we help?</td><td>'.$how_help.'</td></tr>';
    $email_message .= '</table>';
    $email_message .= '</body>';
    $email_message .= '</html>';

    // create email headers

    $headers = "From: ".$first_name." \r\n";
    $headers .= "Bcc: otherguy@otherdomain.com, otherdude@yetanotherdomain.com\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "\r\n";

//      $headers .= 'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    header('Location: results.php');
}
?>

最后,结果.php页面:

<?php 
session_start();
    if (isset($_SESSION['page2']))
    {
        session_destroy();
        header('Location: http://www.domainname.com/thank-you/');
    }
    else
    {
        header('Location: index.php');
    }
?>

2 个答案:

答案 0 :(得分:0)

嗯,我必须承认,代码有点奇怪。正如我所看到的,表单页面中有一个PHP部分,用于测试表单中的值。而已? 我建议在Javascript中执行Rexp。使用它你将有一个更干净的代码:一个表格用JS进行测试,提交到流程页面。 在流程页面中,如果需要,您可以执行另一个测试,如果是faillure,请填写会话并返回到表单(在这种情况下,您必须阅读会话)。

也许在重新提交之前的确认来自页面自我调用的事实。

此致 彼得

答案 1 :(得分:0)

简短:你不能把它关掉。

Long:您应该将表单处理移到开头。

首先检查表单是否已提交,如果是,则处理数据。 另一方面,当表单未提交时,您只需显示表单。

这里的想法是当你发现处理数据时出错, 您可以显示错误消息并再次显示该表单。 这里的好处是,现在你可以填写正确的表格部分 使用$ _POST值。

这样的事情:

    ...
    <input type="text" name="username" value="<?php echo $_POST["username"]?>" />
    ... 

现在用户只填写有错误并重新提交的用户。 如果通过则继续您的电子邮件部分。

请注意,您应该在使用它之前清理$ _POST数组。 (看:striplashes,trim,htmlspecialchars)

希望我明确表示:)


对不起,已经晚了,所以我举了一个例子说明了我的意思: sample php code