使用 || 的问题 在我的带有 php 变量的 if 语句中(变量意外)

我确定这是一个愚蠢的错误,但我无法在我的代码中找到错误的地方。我有一个简单的页面,我可以在其中选择一个测试,然后进行结算。我希望当我们到达页面时,如果变量$testType不存在,我们会显示测试的选择。一旦我们点击了我们想要的测试,我们就会被重定向到同一个页面,带有变量$testType. 这样我们就可以解决我们的测试了。前 2 个测试具有相同的设置(只有显示测试更改的方式,我在第三页上所做的),所以一旦我验证$testType存在,我想验证if($testType == "japTofr" || $testType == "frTojap")但它不起作用:


if(isset($_POST['testType'])) {


    $testType = $_POST['testType'];



    if($testType == "japTofr" || $testType == "frTojap") {

        ?>

        <form method="post" action="test_verb.php">

            <legend> Paramétrer le test de verbe : </legend>

            <label for="nbr"> Nombre de mot à réviser : </label> <input type="number" name="nbr" id="nbr" max="99" required="#"> <br>

            </p> Que voulez-vous réviser ? </p> 

                <input type="checkbox" name="learned[]" id="pas_appris" value="0"> <label for="pas_appris">Pas appris</label> <br>

                <input type="checkbox" name="learned[]" id="en_cours" value="1"> <label for="en_cours">En cours</label> <br>

                <input type="checkbox" name="learned[]" id="appris" value="2" checked="#"> <label for="appris">Appris</label> <br>

            <p> Réviser des leçons du Minna No Nihongo ? </p>

        </form>

这是我得到的错误:解析错误:语法错误,第 16 行 /Users/Nico/Sites/lesTests/settle_test_verb.php 中的意外“$testType”(T_VARIABLE)


开满天机
浏览 74回答 1
1回答

扬帆大鱼

首先通过这样做清理你的烂摊子。而不是使用 if-elseif-else。使用开关盒方法。<?php&nbsp; &nbsp; if(isset($_POST['testType'])){&nbsp; &nbsp; &nbsp; &nbsp; $testType = $_POST['testType'];&nbsp; &nbsp; &nbsp; &nbsp; switch($testType){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'japTofr':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'frTojap': ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put your&nbsp; HTML FORM here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'conjugation': ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put&nbsp; your conjugation html here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put your HTML here for testType == group (your else condition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else{ ?>&nbsp; &nbsp; &nbsp; &nbsp; //put yout HTML here if no testType has been set&nbsp; &nbsp; <?php&nbsp; &nbsp; }?>或者另一种方式是这样<?php&nbsp; &nbsp; if(isset($_POST['testType'])):&nbsp; &nbsp; &nbsp; &nbsp; $testType = $_POST['testType'];&nbsp; &nbsp; &nbsp; &nbsp; switch($testType):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'japTofr':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'frTojap': ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put your&nbsp; HTML FORM here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'conjugation': ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put&nbsp; your conjugation html here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put your HTML here for testType == group (your else condition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; endswitch;&nbsp; &nbsp; else: ?>&nbsp; &nbsp; &nbsp; &nbsp; //put yout HTML here if no testType has been set&nbsp; &nbsp; <?php&nbsp; &nbsp; endif;?>
打开App,查看更多内容
随时随地看视频慕课网APP