表单$ _SESSION数据问题

时间:2014-02-07 12:24:36

标签: php forms

我设计了一个表单,在发送时检查验证,我正在使用Swiftmailer并且所有验证工作但是我有问题。当我返回联系表单时,如果错误填写错误,那么错误仍然存​​在...

名称是必需的! 需要发送电子邮件!

只有在通过验证时才会出错。

当用户离开并返回到新的表单时,如何刷新页面?

联系表格:

<?php
session_start();


?>
<!doctype html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8" />
    <title>Send a message</title>
</head>
<body>
    <div class="container">
        <div <?php if(isset($_SESSION['form_message'])) { echo 'style="color: green"'; } elseif (isset($_SESSION['form_errors'])) { echo 'style="color: red"'; } ?>>
            <?php
                if(isset($_SESSION['form_message']))
                {
                    echo $_SESSION['form_message'];
                    unset($_SESSION['form_data']);
                }
                elseif(isset($_SESSION['form_errors']))
                {
                    echo '<b>You have the following errors:</b>';
                    echo "<br>";
                    foreach($_SESSION['form_errors'] as $display_err) {
                        echo $display_err . "<br>";
                    }
                }
            ?>
        </div>

        <form name="contact" method="post" action="swift_mail.php">
            <div>
                <label for="name">Full name</label><br />
                <input type="text" name="name" id="name" value="<?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['name'] ; } ?>" />
            </div>
            <div>
                <label for="email">Email Address</label><br />
                <input type="text" name="email" id="email" value="<?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['email'] ; } ?>" />
            </div>
            <div>
                <label for="comment">Comment</label><br />
                <textarea name="comment" id="comment"><?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['comment'] ; } ?></textarea>
                <input type="submit" value="submit" name="submit_msg"/>
            </div>
        </form>
    </div>
</body>
</html>
</code>

swift

<?php
session_start();
require_once 'Swift-5.0.3/lib/swift_required.php';
require 'vendor/autoload.php';

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

    /*
        Validate data before it is posted
    */

        $rule_set = array (
            'name' => array(
                'required'

                ),
            'email' => array(
                'required'

                ),
            'comment' => array(
                'required'

                )
            );
    /*
        Checking Validation
    */

        $validation_result = SimpleValidator\Validator::validate($_POST, $rule_set);
        if ($validation_result->isSuccess() == true ) {

    /*
        Contact Form Information 
    */

        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];

        // Main Point of contact

        $email_address = 'ben@bubbledesign.co.uk';


        // Composed Message 

        $body_msg = "Name: " . $name . "<br>" . "Comments: " .$comment;

            /*
        Swift Mail Transport
    */

        $transport = Swift_MailTransport::newInstance();
        $mail = Swift_Mailer::newInstance($transport);

    /*
        Create the Swift Message
    */

        $message = Swift_Message::newInstance('Subject line')
            ->setFrom($email)
            ->setTo($email_address)
            ->setBody($body_msg, "text/html");
    /*
        Send Swift Message
    */
        $result = $mail->send($message);


            $_SESSION['form_message'] = "Thank you for your message someone will be in touch soon.";
            unset($_SESSION['form_errors']);
            unset($_SESSION['form_data']);
            header('location: contact-form.php');
        } else {
            $_SESSION['form_data'] = $_POST;
            $_SESSION['form_errors'] = $validation_result->getErrors();
            header('Location: contact-form.php');
        }
}


?>

<?php session_start(); ?> <!doctype html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8" /> <title>Send a message</title> </head> <body> <div class="container"> <div <?php if(isset($_SESSION['form_message'])) { echo 'style="color: green"'; } elseif (isset($_SESSION['form_errors'])) { echo 'style="color: red"'; } ?>> <?php if(isset($_SESSION['form_message'])) { echo $_SESSION['form_message']; unset($_SESSION['form_data']); } elseif(isset($_SESSION['form_errors'])) { echo '<b>You have the following errors:</b>'; echo "<br>"; foreach($_SESSION['form_errors'] as $display_err) { echo $display_err . "<br>"; } } ?> </div> <form name="contact" method="post" action="swift_mail.php"> <div> <label for="name">Full name</label><br /> <input type="text" name="name" id="name" value="<?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['name'] ; } ?>" /> </div> <div> <label for="email">Email Address</label><br /> <input type="text" name="email" id="email" value="<?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['email'] ; } ?>" /> </div> <div> <label for="comment">Comment</label><br /> <textarea name="comment" id="comment"><?php if(isset($_SESSION['form_data'])) { echo $_SESSION['form_data']['comment'] ; } ?></textarea> <input type="submit" value="submit" name="submit_msg"/> </div> </form> </div> </body> </html> </code>

0 个答案:

没有答案