系统不是应该做的(不能真正更具体)

时间:2011-07-26 15:52:31

标签: php html

我有一个系统,用户使用表单(简单)发送电子邮件。

HTML表单

<form method="post" action="process.php">

            <label class="fieldLabel">Your email:</label><label class="errorLabel"><?php echo $form->error("email"); ?></label> 
            <input type="text" name="email" maxlength="100" class="email" value="<?php echo $form->value("email"); ?>" />

            <label class="fieldLabel">Your enquiry:</label><label class="errorLabel"><?php echo $form->error("body"); ?></label>
            <textarea name="body" class="enquiry"><?php echo $form->value("body"); ?></textarea>    

            <input type="submit" name="enquiry" class="button" value="Send Message"/>

        </form>

在同一页面上,我有这个if语句

if(isset($_SESSION['enq'])){
       if($_SESSION['enq']){
           echo "<h2>Your message has successfully been sent to Alan Slough.</h2>";
       }
       else{
           echo"<h2>Oops, something went wrong. Please try again.</h2>";
       }
       unset($_SESSION['enq']);
    }

现在,表单的process.php文件指向

class Process{
//class constructor
function Process(){
    if(isset($_POST['enquiry'])){
        $this->enquiry();
    }
}

function enquiry(){
    global $session, $form;
    //Registration attempt
    $retval = $session->enquiry($_POST['email'], $_POST['body']);

    //Successful 
    if($retval == 0){
        $_SESSION['enq'] = true;
        header("Location: contact-us.php");
    }
    //Error found with form
    else if($retval == 1){
        $_SESSION['value_array'] = $_POST;
        $_SESSION['error_array'] = $form->getErrorArray();
        header("Location: contact-us.php");
    }
    //Failed
    else if($retval == 2){
        $_SESSION['enq'] = false;
        header("Location: contact-us.php");
    }
} 

};

现在是发生一切的会话页面

//enquiry being made
function enquiry($email, $body){
    global $form;

    //check email entered
    $field = "email"; 
    if(!$email || strlen($email = trim($email)) == 0){
        $form->setError($field, "* Not entered");
    }

    //check body(s) entered
    $field = "body"; 
    if(!$body || strlen($body = trim($body)) == 0){
        $form->setError($field, "* Not entered");
    }

    //if errors exist, send them back to the user
    if($form->num_errors > 0){
        return 1; //errors with form
    }
    else if($form->num_errors == 0){
        $this->customerEnquiry($email, $body);
            return 0; //successful
    }
    else{
        return 2; //failed
    }
}

//send the enquiry to the account email
function customerEnquiry($email, $body){

    $from = "From: ".$email." <".$email.">";
    $to = "random@email.com";
    $subject = "Website enquiry from ".$email."";
    return mail($to,$subject,$body,$from);
}

好吧我的问题是,如果我没有填写表格,错误就不会回来。另外,如果我没有显示成功文本?

任何人都看到这是如何流动的问题? 希望我错过了一些简单的事情!

谢谢!

2 个答案:

答案 0 :(得分:2)

我注意到了这段代码。

if(isset($_SESSION['enq'])){ // <---This...
   if($_SESSION['enq']){ // <---And This
       echo "<h2>Your message has successfully been sent to Alan Slough.</h2>";
   }
   else{
       echo"<h2>Oops, something went wrong. Please try again.</h2>";
   }
   unset($_SESSION['enq']);
}

如果未设置$ _SESSION ['enq'],那么内部的IF语句将永远不会执行,这意味着您既不会看到成功也不会看到失败消息。

另外,您是否在页面的任何位置开始会话?如果你从未开始会话,那么永远不会设置$ _SESSION ['enq']。

http://www.php.net/manual/en/function.session-start.php

答案 1 :(得分:1)

这是一种非常奇怪的方式。例如,您在发送电子邮件之前显示成功/失败消息。

你复制并粘贴了吗?

执行此操作的常用方法是仅在process.php中使用逻辑,这是您进行验证的地方(如果失败则将消息返回给用户)并最终发送电子邮件。

从长远来看,我认为你最好修改流程,因为我现在很难尝试遵循它。