php .session_start在来自submit.php的contact.php上显示错误

时间:2015-02-25 17:46:40

标签: javascript php jquery forms

我需要表单上的错误从submit.php显示到contact.php的表单页面

我尝试使用Session_Start,但它无效。这是我到目前为止所做的。

Contact.php

<?php
ob_start();
session_start();

if ($_SESSION["error"] != "") {
  echo $_SESSION["error"];
  $_SESSION["error"] = "";
}
?>

<?php echo $error; ?>

Submit.php

<?php
ob_start();
session_start();
?>

<?php
//fields
        $link_address   = 'contact'; // page to redirect to
        $honeypot   = '';
        $error      = '';
        $name       = 'Name';
        $email      = 'Email';
        $comments   = 'Message';

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

        $honeypot   = $_POST['email_confirm'];
        $name       = $_POST['name'];
        $email      = $_POST['email'];
        $comments   = $_POST['comments'];

// honeypot
if($honeypot)
  exit(1);

<?php
//error messages
        if(trim($name) == 'Name') {
            $error = '<div class="error_message">Need your Name</div>';
        } else if(trim($name) == '') {
            $error = '<div class="error_message">Need your Name</div>';

        } else if(trim($email) == 'Email') {
            $error = '<div class="error_message">Need your Email</div>';
        } else if(trim($email) == '') {
            $error = '<div class="error_message">Need your Email</div>';

        } else if(!isEmail($email)) {
            $error = '<div class="error_message">Need a valid email</div>';

        } else if(trim($comments) == 'Message') {
            $error = '<div class="error_message">A Message is required</div>';
        } else if(trim($comments) == '') {
            $error = '<div class="error_message">A Message is required</div>';

        }
        if($error == '') {
            if(get_magic_quotes_gpc()) {
                $comments = stripslashes($comments);
        }


//email address
        $address = "mail@example.com";
//email message     
        $e_subject = 'Web Message from: ' . $name . '.';
        $e_body = "From:    $name\nEmail:   $email \r\n\nMessage:\n$comments\n\n\n";

        $msg = $e_body . $e_content . $e_reply;
        if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"))
        {
//success html page response
         echo "<div id='succsess_page'>";
         echo "<h1>Email Sent Successfully.</h1>";
         echo "<p>Thank you. The following was sent to us. <br/><br/>$name<br/><br/>$email<br/><br/>$comments</p>";
         echo "<a href='$link_address'>CLOSE THIS MESSAGE</a>";
         echo "</div>";
         } else echo "Error. Mail not sent";
        }
    }
        if(!isset($_POST['contactus']) || $error != '') // Do not edit.
        {

?>

<?php }

function isEmail($email) { // Email address verification, do not edit.
return(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,12})$/",$email));
}

?>

正如您所看到的,当用户未在contact.php页面上输入$ name等时,所需输入的$ error应显示:

<?php echo $error; ?>

如果用户没有填写所需的输入字段,如何将来自submit.php代码的$ error(s)显示在contact.php页面上?

1 个答案:

答案 0 :(得分:0)

尝试将此 Submit.php 添加$_SESSION['error'] = $error; //如果$ error不为空。还删除了额外的php标签

 <?php
ob_start();
session_start();

//fields
$link_address   = 'contact'; // page to redirect to
$honeypot   = '';
$error      = '';
$name       = 'Name';
$email      = 'Email';
$comments   = 'Message';

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

    $honeypot   = $_POST['email_confirm'];
    $name       = $_POST['name'];
    $email      = $_POST['email'];
    $comments   = $_POST['comments'];

// honeypot
    if($honeypot)
        exit(1);


//error messages
        if(trim($name) == 'Name') {
            $error = '<div class="error_message">Need your Name</div>';
        } else if(trim($name) == '') {
            $error = '<div class="error_message">Need your Name</div>';

        } else if(trim($email) == 'Email') {
            $error = '<div class="error_message">Need your Email</div>';
        } else if(trim($email) == '') {
            $error = '<div class="error_message">Need your Email</div>';

        } else if(!isEmail($email)) {
            $error = '<div class="error_message">Need a valid email</div>';

        } else if(trim($comments) == 'Message') {
            $error = '<div class="error_message">A Message is required</div>';
        } else if(trim($comments) == '') {
            $error = '<div class="error_message">A Message is  required</div>';

        }

        if (isset($error) && !empty($error)){
                $_SESSION['error'] = $error;    //SET errors for contact.php page to display
                echo $error;
        }else {
                $_SESSION['error'] = '';

            if(get_magic_quotes_gpc()) {
                $comments = stripslashes($comments);
            }


            //email address
            $address = "mail@example.com";
            //email message     
            $e_subject = 'Web Message from: ' . $name . '.';
            $e_body = "From:    $name\nEmail:   $email \r\n\nMessage:\n$comments\n\n\n";

            $msg = $e_body . $e_content . $e_reply;
            if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"))
            {
                //success html page response
                echo "<div id='succsess_page'>";
                echo "<h1>Email Sent Successfully.</h1>";
                echo "<p>Thank you. The following was sent to us. <br/><br/>$name<br/><br/>$email<br/><br/>$comments</p>";
                echo "<a href='$link_address'>CLOSE THIS MESSAGE</a>";
                echo "</div>";
            } else echo "Error. Mail not sent";
        }
    }
if(!isset($_POST['contactus']) || $error != '') // Do not edit.
{

    ?>

<?php 
}

function isEmail($email) { // Email address verification, do not edit.
    return(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,12})$/",$email));
}

?>