Getting a value from a dropbox selection

Hi there,

I am working with a dropbox, trying to get a value from it depending the selection made, for going to another page and proceed according to the selection made.

Dropbox elements are loaded from a MYSQL database, a simple query getting names. Almost all works fine, dropbox shows the element in database table, but nothing happens when I select one of them.
Here the code I am using: the query and the form including the dropbox. I am trying to associate to every dropbox element an option value using $count, in order to be able to identify wich option was selected and proceed accordingly when jump to admin_menu.php. As stated before, I can select dropbox elements, but nothing happens.

Any help would be greatly appreciated.
Thanks a lot!!!

<?php
$SQL = “SELECT School_Name FROM Schools”;
$result = mysql_query($SQL, $db_handle) or die(mysql_error());
?>

    &lt;form action="admin_menu.php" method="post"&gt;
           &lt;select name = School&gt;

<?php
$count = 0;
while ($row = mysql_fetch_array($result)){
echo “<option value=‘$count’>”.$row[‘School_Name’].“</option>”;
$count++;
}
?>
</select>
</form>

You need to have a submit button for the form…


<form action="admin_menu.php" method="post">
<select name = School>
<?php
$count = 0;
while ($row = mysql_fetch_array($result)){
echo "<option value='$count'>".$row['School_Name']."</option>";
$count++;
}
?>
</select>
<input type=submit>
</form>

OR, you need to submit the form using JS and onchange for that element.

But, use a straight submit button for now.

Also, check your table Schools - is there no other unique identifier which you can use to identify the School apart from the name?

Otherwise you are going to have fun trying to fetch School number 12 when your backend does not know what 12 applies to:

Most ppl start off with a table like this:


Schools
======
id | int - autoincrement
School_Name | varchar()

eg


id | School_Name
==============
12 | Big School

ie


<?php
$SQL = "SELECT id, School_Name FROM Schools";
$result = mysql_query($SQL, $db_handle) or die(mysql_error());
?>

<form action="admin_menu.php" method="post">
<select name = School>
<?php
while ($row = mysql_fetch_array($result)){
echo "<option value='". $row['id'] ."'>". $row['School_Name'] ."</option>";
}
?>
</select>
<input type=submit>
</form>

You do not have to have a numerical id in your table, its just that they generally do – if you don’t have one, and you cannot/do not want to change the table we can show you a way of selecting by School_Name but it will involve a bit more work.

HTH

Hi HTH,

Thanks a lot for such a complete answer!!!
About your question: yes, I do have tables with ID’s, so I will be right now implementing the modifications for using them as you indicated.

Thanks again!!!

Hi Cups,

I implemented your suggestion. Now the dropbox is using the id’s!!

One last question please.
When I press the button, website goes to ádmin_menu.php’. How can I get the dropbox selected id using a $_POST variable?
Is it possible even when I have a button also?

Thanks a lot!!!

As your form element for the dropdown is seemingly named School then you access the chosen ID by grabbing it from the POST varaiable which is incoming to your form handler “admin_menu.php”;


echo $_POST['School'] . ' is the chosen school id number';

gotchas:

You did not quote the name of the select element School, it will still work, but is generally frowned upon.
Case matters! school !== School
Your form must use the POST method for the above to work
In future you can examine all the incoming vars by having this line at the top of your form handler.


<?php
// 2 lines of debug you remove when you go live
var_dump($_POST)
echo '<hr />';