tlavelle Posted August 21, 2007 Share Posted August 21, 2007 How do I populate the second listbox in this form based on the selection of the first listbox? The first listbox is populated by this query: $year_query = "SELECT distinct year FROM bench_data"; The second listbox should essentially be populated by this: $industry_query = "SELECT distinct comp_desc FROM comp_desc, benc_data where $_POST['year']=bench_data.year and bench_data.comp_key=comp_desc.comp_key"; I know this is alot to ask, but please EXPLAIN the code. I am new and need the explanation elucidated and not just supplied code that will accomplish the goal. Thanks <?php //initialize session session_start(); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create form 1 queries $year_query = "SELECT distinct year FROM bench_data"; $industry_query = "SELECT distinct comp_desc FROM comp_desc"; // execute form 1 queries $year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error()); $industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($year_result) > 0){ // yes // print them one after another if (!isset($_POST['wercbench1']) and !isset($_POST['year'])) { /*" name=\"wercbench1\">";*/ echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench1\">"; echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">"; echo"<tbody>"; echo" <tr>"; echo" <td>Select the comparison year and group then click next"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"year\">"; while($row = mysql_fetch_row($year_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"industry\">"; while($row = mysql_fetch_row($industry_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo"</td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>"; echo" <td></td>"; echo" </tr>"; echo"</tbody>"; echo"</table>"; echo"</form>"; } } else { // no // print status message echo "No rows found!"; } //Calcualtor variables if(isset($_POST['wercbench1_next'])) { $useryear=$_POST['year']; $_SESSION['sess_year']=$_POST['year']; $userindustry=$_POST['industry']; $_SESSION['sess_ind']=$_POST['industry']; $metric_query = "SELECT metric_desc.met_desc, bench_data.median, bench_data.best_pract FROM metric_desc, bench_data where metric_desc.met_key=bench_data.met_key and $useryear=bench_data.year"; $metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error()); if(!isset($_POST['wercbench2']) and (mysql_num_rows($metric_result)) > 0){ // yes // print them one after another //echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench_next2\" value=\"Compare\"></td></tr></table>"; } // free result set memory mysql_free_result($year_result); mysql_free_result($industry_result); mysql_free_result($metric_result); //mysql_free_result($metric_result); } // close connection mysql_close($connection); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/65981-dynamically-populating-listboxes-based-on-previous-selection/ Share on other sites More sharing options...
MadTechie Posted August 21, 2007 Share Posted August 21, 2007 search the form, this has been ask 3 times in the last week! Quote Link to comment https://forums.phpfreaks.com/topic/65981-dynamically-populating-listboxes-based-on-previous-selection/#findComment-329891 Share on other sites More sharing options...
Psycho Posted August 21, 2007 Share Posted August 21, 2007 Well, there are several ways to handle this and the method you choose will depend on the type of user experience you want to happen. If you want a non-javascript solution, then it will require that the user first select the first option and then submit the page in order to select the appropriate options for the second list. With Javascript you open up several more possibilities: If the number of lists is relatively small I suggest doing a query for all possible values and dumping the data into Javascript arrays and doing all the work within the javascript. If you have a large number of lists: 1. You can use AJAX to fire a Javascript even once the user changes a selection in the first list which will call a PHP page to get the list for the 2nd option which will pass it back to the Javascript to be populated 2. Use Javascript to automatically submit the page when the user makes a change to the first list (or allow the user to do it manually). The PHP processing page will determine that the form is not complete and return the form back tot he user with the already entered values and an updated 2nd list. Quote Link to comment https://forums.phpfreaks.com/topic/65981-dynamically-populating-listboxes-based-on-previous-selection/#findComment-329941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.