PHP表单 - 为什么验证不起作用?

时间:2014-08-02 01:10:52

标签: php html forms validation

我正在尝试使用HTML代码验证一个简单的PHP表单,但它无法正常工作。

这是HTML代码:

 <form action="expresscontactform.php" method="post" name="form1" id="form1">

    <label>Name </label><input id="Name" name="Name" type="text" value="<?php echo $_POST['Name']; ?>">
        <span class="error"> <?php echo $errors[1]; ?> </span> 
    <label>Email </label><input id="Email" name="Email" type="text" value="<?php echo $_POST['Email']; ?>">
        <span class="error"> <?php echo $errors[4]; ?> </span> 
    <label>Phone </label><input id="Phone" name="Phone" type="text" value="<?php echo $_POST['Phone']; ?>">
        <span class="error"> <?php echo $errors[2]; ?> </span> 
    <label>Country of origin</label><input id="Country" name="Country" type="text" value="<?php echo $_POST['Country']; ?>">
        <span class="error"> <?php echo $errors[3]; ?> </span> 
    <label>Message </label><textarea id="message" cols="5" rows="5" name="Message"></textarea>

    <input value="Send" class="send_request_new" type="submit">             
</form>

这是PHP代码(在服务器上):

<?php

if(isset($_POST['send_request_new'])){
    $errors = array();

    if($_POST['Name'] == ''){
        $errors[1] = '<span class="error">Please type your name</span>';
    }else if($_POST['Phone'] == ''){
        $errors[2] = '<span class="error">Please type your phone number</span>';
    }else if($_POST['Country'] == ''){
        $errors[3] = '<span class="error">Please type your country</span>';
    }else{

    $EmailFrom = Trim(stripslashes($_POST['Email'])); 
    $EmailTo = 'webmaster@theacademy.co,' . $EmailFrom;
    $Subject = "Online Application Form";
    $name = Trim(stripslashes($_POST['Name'])); 
    $phone = $_POST['Phone'];
    $country = $_POST['Country'];
    $message = $_POST['Message'];


    $header = 'From: ' . $EmailFrom . " \r\n";
    $header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
    $header .= "Mime-Version: 1.0 \r\n";
    $header .= "Content-Type: text/plain";

    // prepare email body text
    $Body .= "Contact form";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "This is an automatically generated e-mail, to inform you that we received your request. We will contact you as soon as possible.";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Kind regards";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "**********************************************";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "The Academy";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "::::::::::::::::::::::::::";
    $Body .= "\n";
    $Body .= "Your Request:";
    $Body .= "\n";
    $Body .= "::::::::::::::::::::::::::";
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Name: ";
    $Body .= $name;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Phone: ";
    $Body .= $phone;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Country: ";
    $Body .= $country;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $EmailFrom;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $message;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Sent on " . date('d/m/Y', time());
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Last visited page: ";
    $Body .= $_SERVER['HTTP_REFERER'];

    if(mail($EmailTo, $Subject, $Body, $header)){
            $result = '<div class="result_ok">Email sent successfully</div>';
            // If successfully we reset all the fields
            $_POST['nombre'] = '';
            $_POST['email'] = '';
            $_POST['asunto'] = '';
            $_POST['mensaje'] = '';

            header("refresh:3;url=http://www.myexample.co/");
        }else{
            $result = '<div class="result_fail">Error!!</div>';
        }
    }
}

?>

我认为下面这行中有一个错误:

<span class="error"> <?php echo $errors[1]; ?> </span> 

但我不知道到底在哪里。

还有一个问题,我如何将“学院”设置为电子邮件发件人?

我不知道这是否是验证表单的最佳方式,如果有人告诉我其他方式我会感谢学习。

如果有人能帮助我,我很感激。

提前致谢。

1 个答案:

答案 0 :(得分:5)

首先,您的代码的执行依赖于此条件语句if(isset($_POST['send_request_new'])),它正在寻找一个名为&#34;命名的&#34;因此,名为send_request_new的元素永远不会执行。

您现在的(未命名)提交按钮

<input value="Send" class="send_request_new" type="submit">

这应该改为:

<input value="Send" class="send_request_new" type="submit" name="send_request_new">

你有一个以这种方式命名的类,而不是name

为了使其正常工作,您需要将整个HTML / PHP放在同一页面中并使用action=""

我注意到您没有电子邮件和邮件,只有姓名/电话号码和国家/地区。


另外,要为发件人的姓名使用个性化方法:

基于以下内容:

$Name = "The Academy";
$email = "email@example.com";

$header = "From: ". $Name . " <" . $email . ">\r\n";

您可以使用PHP.net上的某些过滤器来验证和防止XSS注入:

- 的 http://php.net/manual/en/filter.filters.validate.php

其中一个是FILTER_VALIDATE_EMAIL