0

I'm trying to get the selected value of a dropdown menu and store it in a variable, but i'm getting an 'index undefined' error.

I want to be able to take the data taken from the dropdown menu and push the data to a table based on the selected value.

<form id='dropdownform' method='post' name='dropdownform'>
**<select name='dropdownlist' id='dropdownlist'>**
<option value=''> --All Years-- </option>
<?php

$connection = mysqli_connect("localhost", "root", "");
mysqli_select_db($connection,"6nationsDB");
$dropdownlist = mysqli_query($connection, "SELECT * FROM results GROUP BY year");
while($row = mysqli_fetch_array($dropdownlist)) {
    print("<option value='" . $row[year] . "'>" . $row[year] . "</option>");
}
?>
</form>
<?php

print("</select>");
print("</form>");
print('<table>');
print('<tr>');
print('<th>Round</th>');
print('<th>Date</th>');
print('<th></th>');
print('<th>Home Team</th>');
print('<th></th>');
print('<th></th>');
print('<th></th>');
print('<th>Away Team</th>');
print('<th></th>');
print('<th>Venue</th>');
print('</tr>');

**$select_val = $_POST['dropdownlist'];**
if($select_val == '') {
    $resultset = mysqli_query($connection, "SELECT * FROM results ORDER BY rnd, date");
}
else {
    while ($row=mysqli_fetch_array($resultset)) {

                print('<tr>');
                print('<td>' . $row['rnd'] . '</td>');
                print('<td>' . $row['date'] . '</td>');

                $homeTeamResults = mysqli_query($connection, "select * from teams where id = " . $row['HomeTeamID']);
                $row2 = mysqli_fetch_array($homeTeamResults);

                print('<td>' . "<img src='images/" . $row['HomeTeamID'] . ".png' height='15px'>" . '</td>');
                print('<td>' . $row2['name'] . '</td>');

                print('<td>' . $row['HomeTeamScore'] . '</td>');
                print('<td>-</td>');
                print('<td>' . $row['AwayTeamScore'] . '</td>');

                $awayTeamResults = mysqli_query($connection, "select * from teams where id = " . $row['AwayTeamID']);
                $row3 = mysqli_fetch_array($awayTeamResults);

                print('<td>' . $row3['name'] . '</td>');
                print('<td>' . "<img src='images/" . $row['AwayTeamID'] . ".png' height='15px'>" . '</td>');
                print('<td>' . $row['venue'] . '</td>');

            }
}

        print('</table>');

?>[![this is the error message][1]][1]

any and all help would be appreciated, I'm quite new to PHP

  • If `$_POST['dropdownlist']` produces an undefined index error then that value wasn't posted to the server. How have you confirmed that it's being posted? – David Jan 05 '18 at 15:08
  • This is due to not passing value in option. Pass the value in the option tag. Try echoing value once and test it – Rajesh Paudel Jan 05 '18 at 15:09
  • I know the dropdown list is pulling data from the server because the dropdown list is populated from the database – Kieran Rippy Thompson Jan 05 '18 at 15:18
  • @KieranRippyThompson: Whether or not the ` – David Jan 05 '18 at 15:47

0 Answers0