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

我想在php中对表单中发布的值执行逻辑操作,php,html,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-la

这里是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>

是的,我将参加江户州的活动。
是的,我有兴趣得到Asoebi。
是的,我对住宿很感兴趣。
提交
这里是处理表单数据的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";
    }
}
?>

选中该框时,$\u POST[名称非id]返回值。(对我来说)有些恼人的是,未选中的复选框根本不会返回任何内容。因此,以出席为例:

<input class="form-check-input" type="checkbox" name="attend" value="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";
} }?>

您使用的是单个“=”作为赋值,使用==或===进行比较,例如:
如果($\u POST['attent']='yes'&&$\u POST['trad']='yes'&&$\u POST['house']='yes')
应该是:
如果($\u POST['attent']='yes'&$\u POST['trad']='yes'='yes'&$\u POST['house']='yes'))
阅读此文以供参考:@lovelace它仍然会给我“==”的错误,告诉我们您是如何实现它们的。更新了代码@MagnusErikssonI不确定这就是为什么我会收到错误@WordDragon给我几分钟,我会改进我的答案。更好的答案,我认为,在这种情况下,您不需要id,通常,我希望保持字段名的唯一性。我已经删除了ID,但它仍然会给我相同的“未定义索引”错误。问题不在于您的ID-而是您的名称和值-您有两个标记为“rsvp”的字段,没有一个标记为“参加”。此外,您还有一个值“Attent”,但您正在检查它是否等于“yes”。您需要使字段名(name=…)唯一,并将值与您要检查的内容对齐。太好了!我认为错误是因为当未选中该框时,$\u POST数组中不存在该框-如果您查看我的答案,我在测试中使用了“empty”或“isset”,这是一种不关心索引是否存在的语言结构,因此不报告错误。实际上,我在自己的代码中打开了所有错误和注意事项,并将它们全部删除,以便在出现真正错误时可以看到。警告。这不是推荐的解决方案。您应该修复错误,而不是隐藏错误。如果你按照@wordragon在接受的答案中的建议去做,你就不会得到任何“未定义的索引”。这很容易。只要设置
isset($\u POST['the-key'])和&$\u POST['the-key']=='yes'
(对每个字段执行此操作),您就可以进行设置。你也可以做
$attain=isset($\u POST['attain'])$_POST['attent']:空if
-s之前进行编码>(对每个输入执行此操作)。然后您只需要使用变量,如
if($attain==“yes”…)
等等,就不会再收到任何警告。
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";
} }?>