douyin9987 2017-01-26 15:19
浏览 21

too long

I'm kinda new in PHP and I've created a form that should validate data and submit an error if any field is blank or incorrect. It doesn't tho. Even if email is wrong or any field is empty and the errors are shown it still sends an email. And the headers are not showing in the message. The only case when the errors are shown and the mail isn't send is the case when all fields are empty. Here's the code:

<?php
$NameErr = $EmailErr = $SubErr = $MessErr = "";
$Name = $Email = $Subject = $Message = "";
$header = "From: " . $Email . "Name: " . $Name . "
";
$header .= "Content-Type: text/plain";
$To = "xxx@gmail.com";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Name"])) {
        $NameErr = "Name is required";
    } else {
        $Name = test_input($_POST["Name"]);
        if (!preg_match("/^[a-zA-Z ]*$/", $Name)) {
            $NameErr = "Only letters and white space allowed!";
        }
    }
    if (empty($_POST["Email"])) {
        $EmailErr = "Email is required";
    } else {
        $Email = test_input($_POST["Email"]);
        if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
            $EmailErr = "Invalid email format";
        }
    }
    if (empty($_POST["Subject"])) {
        $SubErr = "Subject is required";
    } else {
        $Subject = test_input($_POST["Subject"]);
    }
    if (empty($_POST["Message"])) {
        $MessErr = "Message is required";
    } else {
        $Message = test_input($_POST["Message"]);
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Name" name="Name"></p>
    <span class="error"> <?php echo $NameErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Email" name="Email"></p>
    <span class="error"> <?php echo $EmailErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Subject" name="Subject"></p>
    <span class="error"> <?php echo $SubErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Message" name="Message"></p>
    <span class="error"> <?php echo $MessErr; ?></span>
    <p>
        <button class="w3-btn w3-grey w3-padding-large w3-hover-green" type="submit" value="Submit" name="pressed">
            <i class="fa fa-paper-plane"></i> SEND MESSAGE
        </button>
    </p>
</form>
<?php
if (isset($_POST["pressed"])) {
    if (empty($NameErr && $SubErr && $MessErr && $EmailErr)) {
        mail($To, $Subject, $Message, $header);
        echo "Email sent.";
    } else {
        echo "Error.";
    }
}
?> 

Can you help me? Error validating is on and it doesn't show me any errors.

  • 写回答

3条回答 默认 最新

  • dongzhen4180 2017-01-26 15:26
    关注

    use isset function instead of empty() to check if the field is posted or not. example:

    if (!isset($_POST["Name"])) {
    ...
    

    also there is no need to check the request method, $_POST will only catch post requests.

    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题