如果变量为空/空

时间:2019-05-06 20:46:34

标签: php variables

我有一个表单提交页面,该页面将字段张贴到使用PHP发送电子邮件的确认页面,但是当该页面作为独立页面而不是表单提交页面运行时,我一直收到空白电子邮件。表单已“必需”,但是我想在$ email变量为空/空的情况下向PHP添加一条语句以停止该过程。

<?php
    // variables start
    $team = $_POST['team'];
    $manager = $_POST['manager'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipcode = $_POST['zipcode'];
    $classification = $_POST['classification'];
    $registration = $_POST['registration'];
    $division = $_POST['division'];
    // variables end

    // email start
    $subject = "Thank you for registering you team";    
    $message = "<html>...

如果$ email变量为空/空,除了停止该过程外,我还想将用户重定向到我们的主页。

2 个答案:

答案 0 :(得分:0)

您应该可以执行以下操作:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['email'])) {
    // variables start
    $team = $_POST['team'];
    $manager = $_POST['manager'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipcode = $_POST['zipcode'];
    $classification = $_POST['classification'];
    $registration = $_POST['registration'];
    $division = $_POST['division'];
    // variables end

    // email start
    $subject = 'Thank you for registering you team';
    $message = '<html>...';
} else {
    header('Location: https://example.com');
}

答案 1 :(得分:0)

最简单的方法:

...
// variables end

if( ! (!isset($email) || trim($email) === '') ){
  header("Location: homepage.php");
  exit();
}

// email start
...

请注意重定向后的exit()语句:如果没有exit()die(),则PHP脚本可能会继续执行,从而可能导致意外的行为。