-1

 
 <form method="post" class="register-form">
 <input type="radio" name="role" id="student" value="student" required>
                    <label for="student">Student</label>
                    <input type="radio" name="role" id="teacher" value="teacher" required>
                    <label for="teacher">Teacher</label>
                    <hr size="1px" width="800px">
                    <div class="student-form">
                        <label for="class">Class</label>
                        <select name="sclass" id="student-class">
                            <option value="" selected disabled hidden>Class</option>
                            <option value="Te">Te</option>
                            <option value="1e">1e</option>
                            <option value="2nd">2nd</option>
                            <option value="3e">3e</option>
                            <option value="4e">4e</option>
                            <option value="5e">5e</option>
                            <option value="6e">6e</option>
                            <option value="7e">7e</option>
                            <option value="8e">8e</option>
                            <option value="9e">9e</option>
                            <option value="10e">10e</option>
                            <option value="11e">11e</option>
                            <option value="12e">12e</option>
                        </select>
                        </div>
                        </form>

I have this php that involves 2 radio buttons, if one is pressed [...] (check code). everytime I do though, i echo $sclass for example and i get this error message with all of them : Warning: Undefined variable $sclass in C: on line 424. Any idea why the variable sclass and others aren't getting values assigned to them?

if($_POST['role'] == 'student'){
        $sclass = $_POST['sclass'];
        $ssection = $_POST['ssection'];
    }
    if($_POST['role'] == 'teacher'){
        $tclass1 = $_POST['tclass1'];
        $tsection1 = $_POST['tsection1'];

        $tclass2 = $_POST['tclass2'] ?? $tclass2 = '';
        $tsection2 = $_POST['tsection2'] ?? $tsection2 = '';
    }
samervjr
  • 29
  • 5
  • Referring to your codes, $sclass will only be assigned if POST["role"] is equal to student (so if POST["role"] is equal to teacher, then $sclass is not assigned) – Ken Lee Jan 19 '21 at 17:13
  • 1
    Please post all relevant code - also the code of the form. Obviously $_POST['sclass'] is empty if you gatr such error. – Alexander Dobernig Jan 19 '21 at 17:14
  • @Ken Lee I added to the tclass2 additional code ?? $tclass2 = ''; which is supposed to assign a null value to tclass2 if not selected, if i add that to the sclass and others will it work? – samervjr Jan 19 '21 at 18:57
  • @AlexanderDobernig I have added all the relevant code to the actual post if you want to check it out. – samervjr Jan 19 '21 at 19:03
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Jan 19 '21 at 21:48

1 Answers1

0

I assume a typo somewhere there was at least one in for "class" instead of "student-class".

I hacked the code above into a bloody prototype and it works. (or phpstorm autofixed the bug :-P )

Questions remeaining

1.) WHAT IS line 424

2.) The form part of tcclass1 and tsclass2 is missing so we do not know anything about them.

One tip: use the Chrome devtools - too see get what is transmitted vie GET and POST + AJAX without having to dump them in the code.

https://i.stack.imgur.com/jau8v.jpg

<?php
if  (!empty($_POST['submit']))

{
if($_POST['role'] == 'student'){
    $sclass = $_POST['sclass'];
    $ssection = $_POST['ssection'];

echo "student Class: {$sclass} Section:{$ssection}" ;

}
if($_POST['role'] == 'teacher'){

    echo " teacher ";
    exit(); //stop here because here after the undefined indices are coming ;-)

    $tclass1 = $_POST['tclass1'];
    $tsection1 = $_POST['tsection1'];

    $tclass2 = $_POST['tclass2'] ?? $tclass2 = '';
    $tsection2 = $_POST['tsection2'] ?? $tsection2 = '';


}

exit();
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>


<form method="post" class="register-form">

    <input type="radio" name="role" id="student" value="student" required>
    <label for="student">Student</label>
        <input type="radio" name="role" id="teacher" value="teacher" required>
        <label for="teacher">Teacher</label>

    <hr size="1px" width="800px">

    <div class="student-form">
        <label for="student-class">Class</label>
        <select name="sclass" id="student-class">
            <option value="" selected disabled hidden>Class</option>
            <option value="Te">Te</option>
            <option value="1e">1e</option>
            <option value="2nd">2nd</option>
            <option value="3e">3e</option>
            <option value="4e">4e</option>
            <option value="5e">5e</option>
            <option value="6e">6e</option>
            <option value="7e">7e</option>
            <option value="8e">8e</option>
            <option value="9e">9e</option>
            <option value="10e">10e</option>
            <option value="11e">11e</option>
            <option value="12e">12e</option>
        </select>

        <input type="text" name="ssection" placeholder="ssection">


        <input type="submit" name ="submit" value="submit">

    </div>
</form>


</body>
</html>
  • 1) echo $sclass; – samervjr Jan 19 '21 at 21:08
  • 2)they are not missing i just didn't put them in the code not to make it too long, but i want the user to be obliged to pick a class1 and section1, and optionally be able to pick a class and section from 2 till 8. will the code you offered make it work? – samervjr Jan 19 '21 at 21:09
  • Then this line is not part of your posted code ?? - at least in my code sclass can be echoed . Is the line 424 in a branch where the script is only coming to if role == student ?? post the complete form and the complete logic if my example does not help you. – Alexander Dobernig Jan 20 '21 at 07:30