如果所选字段没有帖子值,则表单,然后不发送电子邮件

时间:2015-02-09 11:00:14

标签: php forms validation isset

我有以下代码检查是否所有变量都已设置,只有当所有参数都设置为isset()

时才会返回true
if (isset($_POST['FirstName'], $_POST['Surname'], 
$_POST['Telephone'], $_POST['email'], $_POST['PickupPoint'],
$_POST['Destination'], $_POST['StartDate'], 
$_POST['NumberofPassengers'])) {

"if all fields have a POST value then send email"

else{
    echo "Please fill in all fields.";
}

然而,即使例如PickupPoint和Destination未填写,电子邮件也会被发送。

1 个答案:

答案 0 :(得分:3)

这里的问题是空白字段仍然计为设置。你还需要检查它是否为空$_POST['PickupPoint'] !== ''

完整示例:

if (isset($_POST['FirstName'], $_POST['Surname'], $_POST['Telephone'], $_POST['email'], $_POST['PickupPoint'], $_POST['Destination'], $_POST['StartDate'], $_POST['NumberofPassengers']) && $_POST['PickupPoint'] !== '' && $_POST['Destination'] !== '') { ...

虽然我建议检查所有字段