2

I have a drop down menu populated by a MYSQL table. Which I have included the id and the name of a Faculty. I want to post the selected item's id on drop down menu to the next page. Which is addstudent.php. The PHP code is as follows.

        <form action="addstudent.php" method="post">
        <div class="input-group">
            <?php
            echo '<select class="form-control" id="sel1">';
            while ($row = mysqli_fetch_array($result)) {
                echo '<option value = "'.$row['faculty_id'].'">' . $row['name'] . '</option>';
            }
            echo '</select>';

            ?>
            <span class="input-group-btn ml-2">
                <button class="btn btn-primary" type="submit">Add Student</button>
            </span>
        </div>
    </form>

The code on Undefined index is on addstudent.php page. The code is as below

  if (isset($_POST['faculty_id'])) {
      $faculty_id = $_POST['faculty_id'];
  } else{
      echo "Something is wrong";
  }
Tharindu
  • 339
  • 6
  • 19

2 Answers2

3

You missed the name attribute in your select element.

Add a name to the select element like this:

echo '<select class="form-control" id="sel1" name="faculty_id">';

You are getting the post request by using your form post method on second page. So use the name attribute on that page with $_POST super global with given name of the field: $_POST['faculty_id']

IncredibleHat
  • 4,000
  • 4
  • 15
  • 27
Ankit Singh
  • 1,477
  • 1
  • 13
  • 22
1

Try this one:

<form action="addstudent.php" method="post">
    <div class="input-group">
        <?php
        echo '<select class="form-control" id="sel1" name="faculty_id">';
        while ($row = mysqli_fetch_array($result)) {
            echo '<option value = "'.$row['faculty_id'].'">' . $row['name'] . '</option>';
        }
        echo '</select>';

        ?>
        <span class="input-group-btn ml-2">
            <button class="btn btn-primary" name="addstudent" type="submit">Add Student</button>
        </span>
    </div>
</form>

AND your php code has some errors check this one

if (isset($_POST['faculty_id'])) { // OR if(isset($_POST['addstudent']))
    $faculty_id = $_POST['faculty_id'];  // Here the name of select not the value
} else{
    echo "Something is wrong";
}
DopeAt
  • 451
  • 3
  • 15
  • There was no error in his php. What you propose is just a different way to check for form submission. Personally, I think the better way is to use hidden inputs with a static name/value to check for. Relying on a button name and the submit dynamics is janky in the long run. – IncredibleHat Jan 30 '18 at 17:12
  • Yes there was.. He had not the name attribute in echo the select box and was looking the post of select's value – DopeAt Jan 30 '18 at 17:18
  • I repeat. None of his "PHP" was wrong for what he was doing. His only error was missing the `name` on the `select`. Which has nothing to do with php. – IncredibleHat Jan 30 '18 at 17:21
  • So '; while ($row = mysqli_fetch_array($result)) { echo ''; } echo ''; ?> is not php? Sorry then there was not error – DopeAt Jan 30 '18 at 17:22