Question

I have a dropdown box which is populated through MySQL:

echo "<form>";<br>
echo "Please Select Your Event<br />";
echo "<select>";
$results = mysql_query($query)
    or die(mysql_error());
    while ($row = mysql_fetch_array($results)) {
    echo "<option>";
    echo $row['eventname'];
    echo "</option>";
    }   
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";

How do i make it that if one clicks submit it will display a value from a MySQL db

Thanks for the help

Était-ce utile?

La solution

1) Give a name to your <select>, i.e. <select name='event'>.

2) Redirect your form to the display page (and set method POST): <form method='POST' action='display.php'>

3) just display the selected value: <?php echo $_POST['event']; ?>


If you want to use the same page, give a name to your submit button and then do this:

<?php
   if (isset($_POST['submit']))
      echo $_POST['event'];
?>

Hope it helps.

Autres conseils

Just change your query like SELECT result FROM somedb WHERE eventname = '".$eventname."'

Then you just do: (remember to check before while has user already requested info)

The value was: <?php print $row["result"]; ?>

Remember to check $_POST["eventname"] with htmlspecialchars before inserting it to query.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top