保持输入文本值的形式,直到完成

时间:2015-11-10 01:05:09

标签: php html forms

我有一个表单(第二个代码框),当它没有填充时,会出现错误,并刷新页面,从而删除以前表单中的所有数据。什么是将数据保留在表单中的可能解决方案,直到完成并发送电子邮件?

<?php
if ($_POST) {
    if (isset($_POST['contact_us'])) {
        $name = $_POST['full_name'];
        $guest_email = $_POST['email_address'];
        $message = $_POST['message'];
        $empty = false;

        function IsInjected($str) {
            $injections = array('(\n+)',
                '(\r+)',
                '(\t+)',
                '(%0A+)',
                '(%0D+)',
                '(%08+)',
                '(%09+)'
            );
            $inject = join('|', $injections);
            $inject = "/$inject/i";
            if (preg_match($inject, $str)) {
                return true;
            } else {
                return false;
            }
        }

        if (IsInjected($guest_email)) {
            echo '<script language="javascript">';
            echo 'alert("Email")';
            echo '</script>';
        } else if (($name == "FULL NAME") || ($guest_email == "EMAIL ADDRESS")) {
            echo '<div id="overlay">
                    <div id="thank-you">
                        <h6>Missing Information</h6>
                        <p>please fill in the missing information.</p>
                        <button id="close">Close Window</button>
                    </div>
                  </div>';
        } else {
            $email_from = $name; //<== update the email address
            $email_subject = "You have a new message from $name";
            $email_body = "$message \n;" .
                    "Name: $name.\n;" .
                    $to = "email address"; //<== enter personal email here
            $headers = "From: $email_from \r\n";
            $headers .= "Reply-To: $guest_email \r\n";
            //Send the email!
            mail($to, $email_subject, $email_body, $headers);
            //done. redirect to thank-you page.
            //header('Location: enquiry.php');

            echo '<div id="overlay">
                    <div id="thank-you">
                        <h6>Thank You</h6>
                        <p>thank you for your enquiry.</br> a reply will be sent via email with 24 hours.</p>
                        <button id="close">Close Window</button>;
                    </div>
                  </div>';

            $name = '';
            $guest_email = '';
            $message = '';
        }
    }
}
?>

表格

<form method="post" name="enquiry-form" action="" target="_self" autocomplete="on">
    <input type="text" name="full_name" value="FULL NAME"  onFocus="this.value = ''" placeholder="Full Name" />
    <input type="text"  name="email_address" value="EMAIL ADDRESS" onFocus="this.value = ''" placeholder="Email Address" />
    <textarea name="message" placeholder="Enquiry">Enquiry</textarea>
    <button name="contact_us" type="submit">Submit Enquiry</button>
</form>

2 个答案:

答案 0 :(得分:0)

假设表单与动作在同一个php文件上或由php文件调用,您可以只包含超级全局POST值。请注意,您的表单缺少某个操作,这意味着它将转到当前页面。此外,您的输入还有一个onFocus操作,该操作将导致在焦点上删除该值(即,当您单击输入以编辑该值时)

<form method="post" name="enquiry-form" action="" target="_self" autocomplete="on">
            <input type="text" name="full_name" value="<?=htmlspecialchars($_POST['full_name'])?>"  onFocus="this.value = ''" placeholder="Full Name" />
            <input type="text"  name="email_address" value="<?=htmlspecialchars($_POST['email_address'])?>" onFocus="this.value = ''" placeholder="Email Address" />
            <textarea name="message" placeholder="Enquiry"><?=htmlspecialchars($_POST['message'])?></textarea>
            <button name="contact_us" type="submit">Submit Enquiry</button>
</form>

如果已发送电子邮件,则可以清除超级全局$ _POST变量:

$_POST['full_name'] = null;
$_POST['email_address'] = null;
$_POST['message'] = null;

或者你可以只刷新整个$ _POST变量

$_POST = array();

答案 1 :(得分:0)

您可以通过Javascript在每次键输入/字段切换后验证客户端上的输入字段,这会为您提供更多选项。然后你可以使用以下内容:on key up,on blur,当用户发送它时,一切都应该已经消毒了。

这是一个很好的教程,显示了我的意思https://www.developphp.com/video/PHP/Check-User-Sign-Up-Name-Ajax-PHP-Social-Network-Tutorial