khalidorama Posted October 22, 2007 Share Posted October 22, 2007 Dear All, I need to query two tables using two combo boxes. The tables are of this type : author/publications , brand/products ..etc.. Whenever I chose a value in the first ComboBox , the second one should be populated based on the value chosen in the first one. The first combo box is populated, so I can tell that nothing is wrong for the first combo box code. however, when i try to change it's value nothing happens , "the scond one is not populated". I don't know what is the problem.. Maybe it is a mistake to pass javascript variable to php using IEwindow.location.href ?!. If it is a mistake ,then how can i pass javascript variable to php variable???. Please your helppppp !!!! <html> <head> <script language="javascript"> function change(event) { var myindex = form.combo.selectedindex; var brandid = form.combo.options[myindex].value; IEwindow.location.href = "http://mywebsiteaddress/populate.php?brandid=" + brandid; } </script> </head> <body> <?php $host = ""; $user = ""; $pass = ""; $query = "select id,name from brand"; $db = mysql_connect($host,$user,$pass); mysql_select_db("sarabicc_Test",$db); $result = mysql_query($query); if(!$result){die(mysql_error());}; $rows = mysql_num_rows($result); /* function change2() { $myindex = form.combo.selectedindex; $brandid = form.combo.options[$myindex].value; $query2 ="select id,name,brandid from products where brandid= $brandid "; $result2 = mysql_query($query2); $rows2 = mysql_num_rows($result2); for($i=0; $i < $rows; $i++) { $id =mysql_result($result2,$i,"id"); $name =mysql_result($result2,$i,"name"); echo "<option value=\"$id\">$name</option>"; }; } */ echo "<form action =\"populate.php\" name=\"pop\" method=\"get\">"; echo "<select name=\"combo\" onchange=\"change(this);\">"; for($i=0; $i < $rows; $i++) { $id =mysql_result($result,$i,"id"); $name =mysql_result($result,$i,"name"); echo "<option value=\"$id\">$name</option>"; }; echo "</select>"; echo "<select name=\"combo2\" >"; $query2 ="select id,name,brandid from products where brandid= $brandid"; $result2 = mysql_query($query2); $rows2 = mysql_num_rows($result2); for($i=0; $i < $rows2; $i++) { $id =mysql_result($result2,$i,"id"); $name =mysql_result($result2,$i,"name"); echo "<option value=\"$id\">$name</option>"; }; echo "</select>"; echo "</form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/ Share on other sites More sharing options...
enoyhs Posted October 22, 2007 Share Posted October 22, 2007 This won't work with php. Php loads when you load the page so if you want to update "Combo Box" dynamically while user is using them without refreshing page you will need to use simple javascript. You can try and search for examples in google or read some javascript tutorials. Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375293 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 or you can use a onchange="submit();" for your combo boxes and take the variable. or use Ajax on change also. you will need javascript one way or the other. Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375295 Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 There are three methods 1. Javascript only, using preloaded js arrays. 2. AJAX and 3. no javascript at all - submit form with first selection and reload page with second combo loaded based on value in first. See my sig for first 2 methods Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375333 Share on other sites More sharing options...
khalidorama Posted October 22, 2007 Author Share Posted October 22, 2007 hi barand, I took the third solution.. I amended the code .. it worked !! .. great and thanks for your help.. but I still have a small problem, when i chose a value from the first combo box , it is always showing as if i selected the first choice.. however, in the URL , i see that the right selection is processed and also the second combo box is populated based on my selection. It is just that after selecting from the first combo box, i always see the first choice.. how can i solve this ?. i want the user to see what he selected.. <html> <body> <?php $host = "localhost"; $user = "sarabicc_khaled"; $pass = "khaled"; $query = "select id,name from brand"; $db = mysql_connect($host,$user,$pass); mysql_select_db("sarabicc_Test",$db); $result = mysql_query($query); if(!$result){die(mysql_error());}; $rows = mysql_num_rows($result); echo "<form action =\"populate.php\" name=\"pop\" method=\"get\">"; echo "<b>please click to choose brand:</b>"; echo "<select name=\"combo\" onchange=\"submit();\">"; for($i=0; $i < $rows; $i++) { $id =mysql_result($result,$i,"id"); $name =mysql_result($result,$i,"name"); echo "<option value=\"$id\">$name</option>"; }; echo "</select>"; echo "<b>please click to choose product</b>"; echo "<select name=\"combo2\">"; $var = $_REQUEST['combo']; $query2 ="select id,name,brandid from products where brandid= $var"; $result2 = mysql_query($query2); $rows2 = mysql_num_rows($result2); for($i=0; $i < $rows2; $i++) { $id =mysql_result($result2,$i,"id"); $name =mysql_result($result2,$i,"name"); echo "<option value=\"$id\">$name</option>"; }; echo "</select>"; echo "</form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375391 Share on other sites More sharing options...
MadTechie Posted October 22, 2007 Share Posted October 22, 2007 try this <?php echo "please click to choose brand:"; echo "<select name=\"combo\" onchange=\"submit();\">"; for($i=0; $i < $rows; $i++) { $id =mysql_result($result,$i,"id"); $name =mysql_result($result,$i,"name"); $sel = ($_REQUEST['combo'] == $i)?"selected=\"selected\"":""; //ADDED echo "<option value=\"$id\" $sel>$name</option>"; //UPDATED }; echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375400 Share on other sites More sharing options...
khalidorama Posted October 22, 2007 Author Share Posted October 22, 2007 hi, It does not make sense to use $_request() method here, as it is returning the value of the currently selected item from the combo box. What we need in this case, is a function which can return the index of the currently selected item.. do you this function?. or simply , i changed your code so that I can compare values.. instead of comparing to $i , I have to compare to $id.. anyway , thanks a lot for your help.. my problem is solved now . echo "<b>please click to choose brand:</b>"; echo "<select name=\"combo\" onchange=\"submit();\">"; for($i=0; $i < $rows; $i++) { $id =mysql_result($result,$i,"id"); $name =mysql_result($result,$i,"name"); $sel = ($_REQUEST['combo'] == $id)?"selected=\"selected\"":""; //ADDED echo "<option value=\"$id\" $sel>$name</option>"; //UPDATED }; echo "</select>"; Regards Quote Link to comment https://forums.phpfreaks.com/topic/74280-solved-populate-combo-box-based-on-the-value-of-another-one/#findComment-375439 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.