我想对从php中的表单发布的值执行逻辑运算

时间:2018-06-08 21:52:15

标签: php html

这是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";
    }
}
?>

2 个答案:

答案 0 :(得分:1)

勾选此框时,$ _POST [name-not-id]返回值。有点烦人(对我来说),未选中的复选框根本不会返回任何内容。所以,以参加为例:

<input class="form-check-input" type="checkbox" name="attend" value="yes">
当选中此框时,

将在$ _POST [“参加”]中返回“是”,当取消选中该框时,$ _POST中没有值。因此,您可以使用多个测试来查看是否已选中。例如:

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

if (isset($_POST["attend"]) && $_POST["attend"] === "yes") ...

答案 1 :(得分:0)

最后让它发挥作用。

<?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";
} }?>