I want to populate a combo box that depends on the selection of another combo box.
for example, from the first combo i select any name of all user from the database, the second combo automatically fills the address for that user from table of the database.
populate a combo box that depends on the selection of another combo box
Ok.
Have you attempted to try this at all, or read on the subject yet?
Actually I haven't tried it coz i dont know how to start it. I hope you can help me.
Well, PHP cannot dynamically do that, JavaScript is needed. If you want it dynamic, do a google search on your topic, but include javascript.
Otherwise, you can have the form select box use the javascript on.Change function to reload the page posting new variables.
Which way do you prefer?
Ok i think i can use the javascript onChange function.
Another thing is it easier to populate a text box base on the selection of the combo box? All the records will be coming from a database.
You can populate text boxes just as easily as select boxes.
really. If you dont mind. can you show me how to do it?
What's your database look like? Where's your form? I'd be glad to help, but I'm not gonna do it for you.
I have to populate the dropdown menu from the table user which i have done already. If you select a user from the dropdown menu in the onchange it will query from table info where it will get the address of that selected user then place it to a text box.
Thats what i want to do.
Post up your code.
<form name="frm" action="<?=$_SERVER['PHP_SELF']?>?view=cust" method="POST">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0" >
<tr>
<td height="30" colspan="2"><b>CUSTOMER - Enter New Order </b></td>
</tr>
<tr>
<input name="view" type="hidden" value="c_order">
<input name="site_id" type="hidden" value="<?=$site_id?>">
<td width="35%" class="mdm">Customer ID </td>
<td class="sml">
<select name="cust" class="tbox" onChange()>
<?
$res = mysql_query("select * from tblcustomer");
$cnt = mysql_num_rows($res);
$x=0;
while ($x<$cnt)
{
$user_id = mysql_result($res,$x,"user_id");
$lname = mysql_result($res,$x,"lastname");
$fname = mysql_result($res,$x,"firstname");
?>
<option value="<?=$user_id?>" selected><? echo "$fname "; echo "$lname"?></option>
<?
$x++;
}
?>
</select>
</td>
</tr>
<tr>
<td class="mdm">Address:</td>
<td class="mdm">
It will query from the table info
$res = mysql_query("select * from tblinfo where id=$user_id ");
$cnt = mysql_num_rows($res);
<input type="text" name="address" value="The value will be based on the selection and the output of the above query">
</td>
</tr>
Untested. But...you should ge thte idea.
<?php
$load_sql = "SELECT * FROM tblcustomer";
if(isset($_POST['cust']) && is_numeric($_POST['cust'])){
$load_sql .= " WHERE user_id='".$_POST['cust']."'";
}
$main_query = mysql_query($load_sql);
$users = array();
while(($fetch = mysql_fetch_array($main_query)) !== FALSE){
$users[] = $fetch['user_id'];
}
?>
<form name="frm" action="<?=$_SERVER['PHP_SELF']?>?view=cust" method="POST">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0" >
<tr>
<td height="30" colspan="2"><b>CUSTOMER - Enter New Order </b></td>
</tr>
<tr>
<input name="view" type="hidden" value="c_order">
<input name="site_id" type="hidden" value="<?=$site_id?>">
<td width="35%" class="mdm">Customer ID </td>
<td class="sml">
<?php
echo '<select name="cust" onChange="frm.submit();">';
foreach($users as $user){
$sql = mysql_query("SELECT user_id, fname, lname FROM tblcustomers WHERE user_id='".$user['user_id']."'");
$s_fetch = mysql_fetch_array($sql);
$fname = $s_fetch['fname'];
$lname = $s_fetch['lname'];
echo '<option value="'.$user_id.'"';
if($user['user_id'] == $s_fetch['user_id']){
echo ' selected';
}
echo '>'.$fname.' '.$lname.'</option>';
}
echo '</select>';
?>
</td>
</tr>
<tr>
<td class="mdm">Address:</td>
<td class="mdm">
<?php
echo '<input type="text" name="address" value="';
if(($addr = mysql_fetch_array($main_query)) !== FALSE){
echo $addr['address'];
}
echo '">';
?>
</td>
</tr>
</table>
?>
I will try give it a try.
Dude THanks for your help!! I really apreciate it a lot. I've finished the page with your help. Seek you help next tym...
Kudose ROCKS!!!!
Glad you got it all working.