如何对从php表单发布的值执行逻辑操作?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (50)

这里是html

<form name="stream" method="post" action="scripts/validate.php">
  <div class="form-check">
    <input class="form-check-input" type="checkbox" name="attend" value="yes" autocomplete="off">
    <label class="form-check-label" for="attend">Yes, I will attend the event in Edo State.</label>
  </div>
  <div class="form-check">
    <input class="form-check-input" type="checkbox" name="trad" value="yes" autocomplete="off">
    <label class="form-check-label" for="trad">Yes, I'm interested in getting the Asoebi.</label>
  </div>
  <div class="form-check">
    <input class="form-check-input" type="checkbox" name="house" value="yes" autocomplete="off">
    <label class="form-check-label" for="house">Yes, I am interested in getting Accommodation.</label>
  </div>

  <button class="btn bg-bur text-white h4 btn-block mt-20" type="submit" name="stream">Submit</button>
</form>

这里是php处理表单数据,但仍然显示相同的结果,即使当选中1个复选框或者没有选中时,完整注册

<?php 
if( isset( $_POST['stream'] ) ) {    
    if ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] == 'yes' ) {
        echo "I want the full registration";
    }
    elseif ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
        echo "I want the Asoebi and I will attend";
    }
    elseif ($_POST['attend'] == 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
        echo "I am only attending";
    }
    elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
        echo "I only want Asoebi";
    }
    elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
        echo "You haven't selected anything";
    }
}
?>
提问于
用户回答回答于

当选中该框时,$ _POST [name-not-id]会返回值。未选中的复选框完全不会返回任何内容。所以

<input class="form-check-input" type="checkbox" name="attend" value="yes">

在复选框时,将在$ _POST [“attend”]中返回“true”,当复选框未选中时,$ _POST中没有值。所以可以使用几个测试来查看它是否被检查。例如:

if (!empty($_POST["attend"])) ...

要么

if (isset($_POST["attend"]) && $_POST["attend"] === "yes") ...
用户回答回答于
<?php error_reporting(0); if( isset( $_POST['stream'] ) ) {    
if ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] == 'yes' ) {
    echo "I want the full registration";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
    echo "I want the Asoebi and I will attend";
}
elseif ($_POST['attend'] == 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
    echo "I am only attending";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] == 'yes' && $_POST['house'] !== 'yes' ) {
    echo "I only want Asoebi";
}
elseif ($_POST['attend'] !== 'yes' && $_POST['trad'] !== 'yes' && $_POST['house'] !== 'yes' ) {
    echo "You haven't selected anything";
} }?>

扫码关注云+社区

领取腾讯云代金券