0

I'm sure it's a stupid mistake, but i can not find what is wrong in my code. I have a simple page, where I can choose a test, and then settle it. I wanted that when we arrive on the page, if the variable $testType doesn't exists, we display test's choice. Once we click on the test we want, we are redirected to the same page, with the variable $testType. So we can settle our test. The 2 first tests have the same settle (Only the way to display the test change, what I do on a third page), so once i verified $testType exists, I would like to verify if($testType == "japTofr" || $testType == "frTojap") but it's not working :

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>
                <input type="checkbox" name="lesson[]" id="0" value="0"> <label for="0"> Vocabulaire hors MNN </label> <br>
                <input type="checkbox" name="lesson[]" id="1" value="1"> <label for="1"> 1 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="2" value="2"> <label for="2"> 2 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="3" value="3"> <label for="3"> 3 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="4" value="4"> <label for="4"> 4 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="5" value="5"> <label for="5"> 5 </label> <br>
                <input type="checkbox" name="lesson[]" id="6" value="6"> <label for="6"> 6 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="7" value="7"> <label for="7"> 7 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="8" value="8"> <label for="8"> 8 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="9" value="9"> <label for="9"> 9 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="10" value="10"> <label for="10"> 10 </label> <br>
                <input type="checkbox" name="lesson[]" id="11" value="11"> <label for="11"> 11 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="12" value="12"> <label for="12"> 12 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="13" value="13"> <label for="13"> 13 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="14" value="14"> <label for="14"> 14 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="15" value="15"> <label for="15"> 15 </label> <br>
                <input type="checkbox" name="lesson[]" id="16" value="16"> <label for="16"> 16 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="17" value="17"> <label for="17"> 17 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="18" value="18"> <label for="18"> 18 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="19" value="19"> <label for="19"> 19 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="20" value="20"> <label for="20"> 20 </label> <br>
                <input type="checkbox" name="lesson[]" id="21" value="21"> <label for="21"> 21 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="22" value="22"> <label for="22"> 22 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="23" value="23"> <label for="23"> 23 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="24" value="24"> <label for="24"> 24 </label> &nbsp;
                <input type="checkbox" name="lesson[]" id="25" value="25"> <label for="25"> 25 </label> <br>
            <input type="hidden" name="test" value="verb">
            <input type="hidden" name="testType" value="<?php echo $testType; ?>">
            <input type="submit" value="Démarrer le test">
        </form>

    <?php   
    } elseif($testType == "conjugation") {
        ?>
        <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="49" required="#"> <br>
            <p> Temps que vous souhaitez réviser : </p>
            <input type="checkbox" name="tense[]" id="masu_form" value="masu_form"> <label for="masu_form" required="#">Forme en masu</label>
            <input type="checkbox" name="tense[]" id="neg_present" value="neg_present"> <label for="neg_present">Présent négatif</label>
            <input type="checkbox" name="tense[]" id="past" value="past"> <label for="past">Passé</label>
            <input type="checkbox" name="tense[]" id="neg_past" value="neg_past"> <label for="neg_past">Passé négatif</label>
            <input type="checkbox" name="tense[]" id="te_form" value="te_form"> <label for="te_form">Forme en Te</label>
            <input type="checkbox" name="tense[]" id="potential" value="potential"> <label for="potential">Potentielle</label>
            <input type="checkbox" name="tense[]" id="passive" value="passive"> <label for="passive">Passive</label>
            <input type="checkbox" name="tense[]" id="causative" value="causative"> <label for="causative">Causative</label>
            <input type="checkbox" name="tense[]" id="conditional" value="conditional"> <label for="conditional">Conditionnelle</label>
            <input type="checkbox" name="tense[]" id="volitional" value="volitional"> <label for="volitional">Volationnelle</label>
            <input type="checkbox" name="tense[]" id="imperative" value="imperative"> <label for="imperative">Imperative</label>
            <input type="hidden" name="test" value="verb">
            <input type="hidden" name="testType" value="<?php echo $testType; ?>">
            <input type="submit" value="Démarrer le test">
        </form>     

    <?php   
    // $testType == group   
    } else {
        ?>
        <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="49" required="#"> <br>
            <input type="hidden" name="test" value="verb">
            <input type="hidden" name="testType" value="<?php echo $testType; ?>">
            <input type="submit" value="Démarrer le test">
        </form>     
        <?php
    }       

} else {
?>  
    <div class="container">
        <div class="row">
            <div class="offset-4 col-md-4"> Test de verbe </div>
        </div>

        <div class="row">
            <div class="col-md-6"> 
                <p> Traduire du Japonais au Français </p>
                <p> Les verbes sont écrits en kanji + hiragana et il faut donner leur traduction française. </p>
                <form method="post" action="settle_test_verb.php">
                    <input type="hidden" name="testType" value="japTofr">
                    <input type="submit" value="Faire ce test" class="btn btn-primary">
                </form>
            </div>
            <div class="col-md-6"> 
                 Traduire du Français au Japonais 
                <p> Les verbes sont écrits en français et il faut donner leur traduction en romaji. </p>
                <form method="post" action="settle_test_verb.php">
                    <input type="hidden" name="testType" value="frTojap">
                    <input type="submit" value="Faire ce test" class="btn btn-primary">
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6"> 
                 Conjugaison des verbes 
                <p> Choisissez un temps, et conjuguez les verbes dans le temps choisi. </p>
                <form method="post" action="settle_test_verb.php">
                    <input type="hidden" name="testType" value="conjugation">
                    <input type="submit" value="Faire ce test" class="btn btn-primary">
                </form>
            </div>
            <div class="col-md-6"> 
                 Trouver les groupes des verbes  
                <p> Trouver à quel groupe appartiennent les verbes </p>
                <form method="post" action=""> <!-- Le mettre directement sur la page qui traite les parametre du test -->
                    <input type="hidden" name="testType" value="group">
                    <input type="submit" value="Faire ce test" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>

Here is the error i get : Parse error: syntax error, unexpected '$testType' (T_VARIABLE) in /Users/Nico/Sites/lesTests/settle_test_verb.php on line 16

Nicoji_
  • 67
  • 8

1 Answers1

1

First clean up your mess by doing this. instead of using if-elseif-else. go with the switch case method.

<?php
    if(isset($_POST['testType'])){

        $testType = $_POST['testType'];

        switch($testType){
            case 'japTofr':
            case 'frTojap': ?>
                //put your  HTML FORM here

           <?php 

            break;

            case 'conjugation': ?>

                //put  your conjugation html here
            <?php 

            break;

            default: ?>
                //put your HTML here for testType == group (your else condition)
            <?php
        }
    }else{ ?>
        //put yout HTML here if no testType has been set
    <?php
    }
?>

or another way is this

<?php
    if(isset($_POST['testType'])):

        $testType = $_POST['testType'];

        switch($testType):
            case 'japTofr':
            case 'frTojap': ?>
                //put your  HTML FORM here

           <?php 

            break;

            case 'conjugation': ?>

                //put  your conjugation html here
            <?php 

            break;

            default: ?>
                //put your HTML here for testType == group (your else condition)
            <?php
        endswitch;
    else: ?>
        //put yout HTML here if no testType has been set
    <?php
    endif;
?>
Kenneth Sunday
  • 865
  • 7
  • 14
  • native PHP is quite annoying when dealing with this kind of conditions that we can't avoid its existence. So we need to be creative to organize our source code even this kind of mess. – Kenneth Sunday Apr 13 '20 at 08:52
  • Thank you for your return. I'm aware of switch, but I personally learned that switch are to avoid big if-statement, but if-elseif-else was introduce as ok when we have only 3 possibilities, so i didn't know it was consider as a mess. But if it's cleaner using switch here and it can solve my issue, I sure will. Still, for my personal knowledge, what was wrong it my code ? Where the error come from ? – Nicoji_ Apr 13 '20 at 09:01
  • instead of using the DOUBLE BARS for or condition . you can use the "OR" keyword instead . better to read if(CONDITION_1 OR CONDITION_2) rather than if(CONDITION_1 || CONDITION_2). even non-coder can simply understand code by doing that. the more member that can understand your code . the easier your work will :) – Kenneth Sunday Apr 13 '20 at 09:03