Querying a database with a drop down menu php
Hi
Im having a problem querying a database with a dropdown menu with php. What I am trying to do is have the user select an option from the drop down menu hit submit and then put the results of the query on the new page. I have searched all over the internet and all I have been able to find is how to do this with a text field. So far all I have accomplished is opening/closing the connection to the db and opening a new page using the submit button. Any help would be appreciated. Heres the code. "search2.php" [code] <?php $input = $_POST['building']; ?> <!--Set up a table--> <table border="1" cellpadding="5"> <tr> <th>Database ID</th> <th>BUILDING</th> <?php $select = "SELECT * FROM classroom WHERE building =\ "$input\ ""; $result = mysqli_query($link, $select); //Print out the results echo <<<END } ?> <!--End the Table--> </TABLE> <?php mysqli_close($link); ?> [code] site3.php [code] <?php $hostname = "localhost"; $username = "root"; $password = "password"; $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); print "Connected to MySQL<br>"; $select1 = "SELECT building FROM classroom"; $result1 = mysqli_query($link, $select1); // loop through to bring up all the database locations while ($row = mysqli_fetch_array($result1)) { $building = $row['building']; echo "<option>".$building."</option>"; } mysql_close($dbh); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Search for Sections</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <link type="text/css" rel="stylesheet" href="SiteStyle.css"> <link type="text/css" rel="stylesheet" href="ConstituencyStyle.css"> </head> <div id="acctWidget"></div> <div class="custom"></div> <div id="pageBody" class="WBST"> <div id="bodyConstituency">Student</div> <div class="clear"></div> <div id="bodyForm"> <div id="screenTitle"><span>Search for Sections</span></div> <div class="custom"> <div class="customText">Please fill out at least 3 fields: either term or both start and end dates are required fields. The drop down menu for "Subjects" is considered one field. </div></div> <div class="screen"> <form name="datatelform" method="post" action="search.php"> <table class="mainTable" summary="This panel contains controls that are vertically aligned."> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td class="component combobox label"> <label id="LABELID_VAR1" class="" for="VAR1"> Term</label></td> <td class="control left"> <div> <label class="hidden" for="VAR1"> </label> <select name="VAR1" id="VAR1"> <option value=""></option> <option value="2006Reg" selected="Y"> 2006 Fall Regular </option> <option value="2006Ours">2006 Fall Our Scheduler </option> </select></div></td> </tr> </table></div><HR></td> </tr> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td colspan="2"> <div class="horizontal"> <table class="layout" summary="This panel contains controls that are horizontally aligned."> <tr> <td class="component date label"> <label id="LABELID_DATE_VAR1" class="" for="DATE_VAR1"> Starting On/After Date</label></td> <td class="control right"> <div> <label class="hidden" for="DATE_VAR1"> </label> <input name="DATE.VAR1" id="DATE_VAR1" type="text" maxlength="12" size="12"></div> </td> <td class="component date label"> <label id="LABELID_DATE_VAR2" class="" for="DATE_VAR2"> Ending By Date</label></td> <td class="control right"> <div> <label class="hidden" for="DATE_VAR2"> </label> <input name="DATE.VAR2" id="DATE_VAR2" type="text" maxlength="12" size="12"></div> </td> </tr> </table></div></td> </tr> </table></div><HR></td> </tr> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td colspan="2"> <div class="envisionWindow" id="GROUP_Grp_LIST_VAR1"> <input type="hidden" value="LIST.VAR1" name="LIST.VAR1_CONTROLLER"> <input type="hidden" value="LIST.VAR1*LIST.VAR3*LIST.VAR4" name="LIST.VAR1_MEMBERS"> <table summary="Class Sections Information"> <tr> <th class="groupTitle" colspan="3"> Class Sections Information</th> </tr> <tr> <th class="windowIdx"> </th> <th class="Grp_LIST_VAR1 left"> <input type="hidden" name="LIST.VAR1_MAX" value="5"> Subjects </th> <th class="Grp_LIST_VAR1 left"> <input type="hidden" name="LIST.VAR3_MAX" value="5"> Course Number </th> <th class="Grp_LIST_VAR1 left"> <input type="hidden" name="LIST.VAR4_MAX" value="5"> Section </th> </tr> <tr> <td class="windowIdx">1</td> <td class=" LIST_VAR1 left oddrow"> <div> <label class="hidden" for="LIST_VAR1_1"> </label> <select name="LIST.VAR1_1" id="LIST_VAR1_1"> <option value=""> </option> <option value="ACCT"> ACCT - Accounting </option> </select></div> </td> <td class=" LIST_VAR3 left oddrow"> <div> <label class="hidden" for="LIST_VAR3_1"> </label> <input name="LIST.VAR3_1" id="LIST_VAR3_1" type="text" maxlength="7" size="7"></div> </td> <td class=" LIST_VAR4 left oddrow"> <div> <label class="hidden" for="LIST_VAR4_1"> </label> <input name="LIST.VAR4_1" id="LIST_VAR4_1" type="text" maxlength="5" size="5"></div> </td> </tr> <tr> <td class="windowIdx">2</td> 2<td class=" LIST_VAR1 left evenrow"> <div> <label class="hidden" for="LIST_VAR1_2"> </label> <select name="LIST.VAR1_2" id="LIST_VAR1_2"> <option value=""> </option> <option value="ADVG"> ADVG - Advertising </option> </select></div> </td> <td class=" LIST_VAR3 left evenrow"> <div> <label class="hidden" for="LIST_VAR3_2"> </label> <input name="LIST.VAR3_2" id="LIST_VAR3_2" type="text" maxlength="7" size="7"></div> </td> <td class=" LIST_VAR4 left evenrow"> <div> <label class="hidden" for="LIST_VAR4_2"> </label> <input name="LIST.VAR4_2" id="LIST_VAR4_2" type="text" maxlength="5" size="5"></div> </td> </tr> <tr> <td class="windowIdx">3</td> <td class=" LIST_VAR1 left oddrow"> <div> <label class="hidden" for="LIST_VAR1_3"> </label> <select name="LIST.VAR1_3" id="LIST_VAR1_3"> <option value=""> </option> <option value="ACCT"> <option value="AENG"> AENG - Aero Engineering </option> </select></div> </td> <td class=" LIST_VAR3 left oddrow"> <div> <label class="hidden" for="LIST_VAR3_3"> </label> <input name="LIST.VAR3_3" id="LIST_VAR3_3" type="text" maxlength="7" size="7"></div> </td> <td class=" LIST_VAR4 left oddrow"> <div> <label class="hidden" for="LIST_VAR4_3"> </label> <input name="LIST.VAR4_3" id="LIST_VAR4_3" type="text" maxlength="5" size="5"></div> </td> </tr> <tr> <td class="windowIdx">4</td> <td class=" LIST_VAR1 left evenrow"> <div> <label class="hidden" for="LIST_VAR1_4"> </label> <select name="LIST.VAR1_4" id="LIST_VAR1_4"> <option value=""> </option> <option value="ANTH"> ANTH - Anthropology </option> </select></div> </td> <td class=" LIST_VAR3 left evenrow"> <div> <label class="hidden" for="LIST_VAR3_4"> </label> <input name="LIST.VAR3_4" id="LIST_VAR3_4" type="text" maxlength="7" size="7"></div> </td> <td class=" LIST_VAR4 left evenrow"> <div> <label class="hidden" for="LIST_VAR4_4"> </label> <input name="LIST.VAR4_4" id="LIST_VAR4_4" type="text" maxlength="5" size="5"></div> </td> </tr> <tr> <td class="windowIdx">5</td> <td class=" LIST_VAR1 left oddrow"> <div> <label class="hidden" for="LIST_VAR1_5"> </label> <select name="LIST.VAR1_5" id="LIST_VAR1_5"> <option value=""> </option> <option value="CHEM"> CHEM - Chemistry </option> </select></div> </td> <td class=" LIST_VAR3 left oddrow"> <div> <label class="hidden" for="LIST_VAR3_5"> </label> <input name="LIST.VAR3_5" id="LIST_VAR3_5" type="text" maxlength="7" size="7"></div> </td> <td class=" LIST_VAR4 left oddrow"> <div> <label class="hidden" for="LIST_VAR4_5"> </label> <input name="LIST.VAR4_5" id="LIST_VAR4_5" type="text" maxlength="5" size="5"></div> </td> </tr> </table></div></td> </tr> </table></div><HR></td> </tr> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td colspan="2"> <div class="horizontal"> <table class="layout" summary="This panel contains controls that are horizontally aligned."> <tr> <td class="component combobox label"> <label id="LABELID_VAR7" class="" for="VAR7"> Sections Meeting After</label></td> <td class="control left"> <div> <label class="hidden" for="VAR7"> </label> <select name="VAR7" id="VAR7"> <option value=""> </option> <option value="05:00"> 05:00 - 5am </option> <option value="06:00"> 06:00 - 6am </option> <option value="07:00"> 07:00 - 7am </option> <option value="08:00"> 08:00 - 8am </option> <option value="09:00"> 09:00 - 9am </option> <option value="10:00"> 10:00 - 10am </option> <option value="11:00"> 11:00 - 11am </option> <option value="12:00"> 12:00 - 12pm </option> <option value="13:00"> 13:00 - 1pm </option> <option value="14:00"> 14:00 - 2pm </option> <option value="15:00"> 15:00 - 3pm </option> <option value="16:00"> 16:00 - 4pm </option> <option value="17:00"> 17:00 - 5pm </option> <option value="18:00"> 18:00 - 6pm </option> <option value="19:00"> 19:00 - 7pm </option> <option value="20:00"> 20:00 - 8pm </option> <option value="21:00"> 21:00 - 9pm </option> <option value="22:00"> 22:00 - 10pm </option></select></div> </td> <td class="component combobox label"> <label id="LABELID_VAR8" class="" for="VAR8"> Sections Ending Before</label></td> <td class="control left"> <div> <label class="hidden" for="VAR8"> </label> <select name="VAR8" id="VAR8"> <option value=""> </option> <option value="05:00"> 05:00 - 5am </option> <option value="06:00"> 06:00 - 6am </option> <option value="07:00"> 07:00 - 7am </option> <option value="08:00"> 08:00 - 8am </option> <option value="09:00"> 09:00 - 9am </option> <option value="10:00"> 10:00 - 10am </option> <option value="11:00"> 11:00 - 11am </option> <option value="12:00"> 12:00 - 12pm </option> <option value="13:00"> 13:00 - 1pm </option> <option value="14:00"> 14:00 - 2pm </option> <option value="15:00"> 15:00 - 3pm </option> <option value="16:00"> 16:00 - 4pm </option> <option value="17:00"> 17:00 - 5pm </option> <option value="18:00"> 18:00 - 6pm </option> <option value="19:00"> 19:00 - 7pm </option> <option value="20:00"> 20:00 - 8pm </option> <option value="21:00"> 21:00 - 9pm </option> <option value="22:00"> 22:00 - 10pm </option></select></div> </td> </tr> </table></div></td> </tr> </table></div><HR></td> </tr> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td colspan="2"> <div class="horizontal"> <table class="layout" summary="This panel contains controls that are horizontally aligned."> <tr> <td class="component checkbox label"> <label id="LABELID_VAR10" class="" for="VAR10"> Mon</label></td> <td class="control left"> <div> <label class="hidden" for="VAR10"> </label> <input name="VAR10" id="VAR10" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR11" class="" for="VAR11"> Tue</label></td> <td class="control left"> <div> <label class="hidden" for="VAR11"> </label> <input name="VAR11" id="VAR11" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR12" class="" for="VAR12"> Wed</label></td> <td class="control left"> <div> <label class="hidden" for="VAR12"> </label> <input name="VAR12" id="VAR12" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR13" class="" for="VAR13"> Thu</label></td> <td class="control left"> <div> <label class="hidden" for="VAR13"> </label> <input name="VAR13" id="VAR13" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR14" class="" for="VAR14"> Fri</label></td> <td class="control left"> <div> <label class="hidden" for="VAR14"> </label> <input name="VAR14" id="VAR14" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR15" class="" for="VAR15"> Sat</label></td> <td class="control left"> <div> <label class="hidden" for="VAR15"> </label> <input name="VAR15" id="VAR15" type="checkbox" value="Y"></div> </td> <td class="component checkbox label"> <label id="LABELID_VAR16" class="" for="VAR16"> Sun</label></td> <td class="control left"> <div> <label class="hidden" for="VAR16"> </label> <input name="VAR16" id="VAR16" type="checkbox" value="Y"></div> </td> </tr> </table></div></td> </tr> </table></div><HR></td> </tr> <tr> <td colspan="2"> <div class="vertical"> <table class="layout" summary="This panel contains controls that are vertically aligned."> <tr> <td class="component text label"> <label id="LABELID_VAR3" class="" for="VAR3"> Course Title Keyword(s)</label></td> <td class="control left"> <div> <label class="hidden" for="VAR3"> </label> <input name="VAR3" id="VAR3" type="text" maxlength="30" size="30"></div> </td> </tr> <tr> <td colspan="2"> <div class="horizontal"> <table class="layout" summary="This panel contains controls that are horizontally aligned."> <tr> <td class="component combobox label"> <label id="LABELID_VAR6" class="" for="VAR6"> Location</label></td> <td class="control left"> <div> <label class="hidden" for="VAR6"> </label> <select name="VAR6" id="VAR6"> <option value=""> </option> <option value="MA" selected="Y"> MA - Manhattan </option> </select></div> </td> <td class="component combobox label"> <label id="LABELID_VAR21" class="" for="VAR21"> Academic Level</label></td> <td class="control left"> <div> <label class="hidden" for="VAR21"> </label> <select name="VAR21" id="VAR21"> <option value=""> </option> <option value="UG" selected="Y"> UG - Undergraduate </option></select></div> </td> </tr> </table></div></td> </tr> <tr> <td class="component text label"> <label id="LABELID_VAR9" class="" for="VAR9"> Instructor's Last Name</label></td> <td class="control left"> <div> <label class="hidden" for="VAR9"> </label> <input name="VAR9" id="VAR9" type="text" maxlength="20" size="20"></div> </td> </tr> </table></div><HR></td> </tr> </table> <div class="submit"> <input type="hidden" name="SUBMIT_OPTIONS" value=""> <input type="hidden" name="RETURN.URL" value="https://www.nyit.edu/webadvisor/webadvisor?TOKENIDX=3514951627&type=M&constituency=WBST&pid=CORE-WBST"> <input type="submit" onclick="javascript:disableAndSubmit(this);" onkeypress="javascript:disableAndSubmit(this);" name="SUBMIT" value="SUBMIT" accesskey="S"></div> </form></div></div></div> <div id="pageFooter"> <div class="custom"></div> <div id="footerBanner"> <div id="sysToolbar"> <div id="sysWidget"></div></div></div></div> </body> </html> [code] |
My PHP knowledge is non-existant, but my HTML and Perl/CGI knowledge is pretty good, so, I'll try my best here.
I'm almost certain that you can set a "VALUE" or "NAME" field for each item in the drop-down menu, and then what you'll have to figure out, in the PHP code, is how to funnel that VALUE or NAME into the query, and deal with it from there. It will almost certainly require some more PHP writing on your behalf (IF-THEN conditions for the NAME/VALUE field and translating the drop-down choices into the desired database query). The reason you've probably only been able to find textfield help pages is because textfields are the most difficult thing to code for, as the end user can type in, literally, anything. :D |
You have an html form, with the method post and the action search.php. When the form is submitted control will be sent to the file search.php. In that file the variables will be held in the $_POST super global. The easiest way to see what is happening is to put in the following code.
PHP Code:
graeme. |
Yo indieNick
This is what I understood from what you said. I declared some global php variables. Where LIST.VAR1_1 stores every option in the drop down menu I said if LIST.VAR1_1 == wutever you select in the drop down menu then the global_php_variable = an sql query of wutever is selected in the menu Could you tell me if I am on the right track? Also does anyone know how to output the result of the query on the next page? Thanks. |
You are most definitely on the right track. :D
As for outputting the result of the query, I couldn't even begin to give you any PHP code to do so, but what you'll need to do (in theory) is to trap the returned values from the SQL server and display them accordingly. |
A Example PHP !
I can some code lines to help you : ( a simple way)
<?php $hostname = "localhost"; $username = "root"; $password = "password"; $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); print "Connected to MySQL<br>"; $select1 = "SELECT id,building FROM classroom"; $result1 = mysqli_query($link, $select1); ?> <!--------------------------------> <html> <head> <title>example</title> </head> <body> <form action = "search.php" method = "POST"> <select name = "txtBuilding"> <!--------------------------------> <?php // loop through to bring up all the database locations while ($row = mysqli_fetch_array($result1)) { $building = $row['building']; $id = $row['id]; echo "<option value=".$id.">".$building."</option>"; } mysql_close($dbh); ?> <!--------------------------------> </select> <input type="submit" name="txtSearch" style="width:70px;"/> </form> </body> </html> If you have any complain , please contact me ! (thaihoabo@yahoo.com) |
All times are GMT -5. The time now is 05:11 AM. |