douyonglang4845 2018-06-08 21:52
浏览 41
已采纳

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

Here is 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>

And here is the php to process the form data but it keeps displaying the same result, "I want the full registration" even when 1 checkbox is checked or none is checked

<?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条回答 默认 最新

  • douyingtai6662 2018-06-08 22:11
    关注

    When you check the box, $_POST[the name-not-id] comes back with the value. Somewhat annoyingly (to me), checkboxes that are unchecked return nothing at all. So, using the attend as an example:

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

    would return "yes" in $_POST["attend"] when the box is checked, no value in $_POST when the box is unchecked. So you could use several tests to see if it is checked. For example:

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

    or

    if (isset($_POST["attend"]) && $_POST["attend"] === "yes") ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计