I am using PHP to retrieve values from a database but I have struck a problem. When I select a value from the drop-down list and press submit the value which I put in is not selected. All of the other elements in the form work perfectly. I am not sure what I am doing wrong.
When using isset, the end result shows nothing, not what I selected in the dropdown menu.
Here is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "timedrun";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
?>
<html>
<head>
<title>Add Run</title>
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
<link rel='stylesheet' type='text/css' href='mystyle.css'>
</head>
<body>
<h1>Timed Run</h1>
<center>
<hr>
<form action='#' method='post'>
Select Student: <select name='studentSelected' class="form">
<?php
$fsql = "SELECT firstName FROM students";
$fresult = $conn->query($fsql);
$lsql = "SELECT lastName FROM students";
$lresult = $conn->query($lsql);
if ($fresult->num_rows > 0) {
while ($row = $fresult->fetch_assoc()){
echo '<option value="'.$row['ID'].'">' .$row['firstName'].' </option>';
}
};
?>
</select>
<br><a class="space" ></a><br>
Time (s): <input type="int" name="time" class="form">
<br><a class="space"></a><br>
Select Date: <input type="date" name="date" class="form">
<br><a class="space"></a><br>
Notes: <input type="text" name="notes" class="form">
<br><a class="space"></a><br>
<input type="submit" class="form" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['studentSelected'];
echo "You have selected :" .$selected_val;
}
?>
</center>
</body>
</html>