使用必填字段处理我的表单

时间:2014-05-19 20:11:21

标签: php forms if-statement

我刚开始学习php,所以我怀疑这将是一个简单的问题。我想创建一个具有一个必填字段(名称)并且至少需要两个其他字段(电子邮件或电话号码)之一的表单。这就是我想出的:

<?php
if (isset($_POST["submit"])) {

    if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {     
            $error = "Your name and a contact method are required fields";}

    else {
        $name = $_POST["name"];
        $email = $_POST["email"];
        $number = $_POST["number"];
        $contactmethod = $_POST["contactmethod"];
        $subject = $_POST["subject"];
        $location = $_POST["location"];
        $message = $_POST["message"];
        $subjectline = "You have a message from $name!";
        $header = "From: $email";

$body = <<<EMAIL

Name: $name,
Number: $number, 
Email: $email, 
Subject: $subject,
Location: $location,
Contact method: $contactmethod.

$message

EMAIL;

mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
} 
?>

我不知道出了什么问题。

由于

2 个答案:

答案 0 :(得分:0)

您不能将括号与括号互换使用。

if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {     

需要使用括号

if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {     

答案 1 :(得分:0)

您的问题:您在if语句中使用了{和}。

下次你可以使用像http://phpcodechecker.com/

这样的PHP代码检查器

正确的代码:     

    if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {     
            $error = "Your name and a contact method are required fields";}

    else {
        $name = $_POST["name"];
        $email = $_POST["email"];
        $number = $_POST["number"];
        $contactmethod = $_POST["contactmethod"];
        $subject = $_POST["subject"];
        $location = $_POST["location"];
        $message = $_POST["message"];
        $subjectline = "You have a message from $name!";
        $header = "From: $email";

$body = <<<EMAIL

Name: $name,
Number: $number, 
Email: $email, 
Subject: $subject,
Location: $location,
Contact method: $contactmethod.

$message

EMAIL;

mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
} 
?>