0

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

3
  • Do you want it to display the value on the page that submitted was clicked on, or on the page that the submit button leads to? Commented Sep 26, 2011 at 12:20
  • on the same page would be better. Basically the value for the dropdown is $row['eventname']; When selected or submited it displays the value $row['result']; Those two values are already connected using a SQL query. Commented Sep 26, 2011 at 12:26
  • Just an FYI, it may be easier to just add the loop between the <select>..</select> tags in regular HTML, rather than echoing each tag -- unless that was just for posting on here. Also, may want to check out jQuery's Ajax capabilities - www.jquery.com Commented Sep 26, 2011 at 13:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

So i can post a value to the same page it came from. Then the page just reloads and i pull the value from the post?
But remember to use htmlspecialchars before printing anything. Otherwise there is risk of XSS attack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.