Jump to content

[SOLVED] Populate Combo Box based on the value of another one!


Recommended Posts

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>

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.

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

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>

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>";
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.