首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我想对从php中的表单发布的值执行逻辑操作。

我想对从php中的表单发布的值执行逻辑操作。
EN

Stack Overflow用户
提问于 2018-06-09 05:52:15
回答 2查看 32关注 0票数 0

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-09 06:11:33

当您选中该框时,$_POSTthe name-not-id将返回该值。有些恼人的是(对我来说),未选中的复选框根本不会返回任何内容。因此,以an为例:

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

如果选中此框,则在$_POST“the”中返回"yes“;如果未选中此框,则在$_POST中不返回任何值。因此,您可以使用多个测试来查看它是否被选中。例如:

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

if (isset($_POST["attend"]) && $_POST["attend"] === "yes") ...
票数 1
EN

Stack Overflow用户

发布于 2018-06-09 08:19:26

终于让它起作用了。

<?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";
} }?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档