Jump to content

Recommended Posts

Hi

I write this code now I want connect 2 list box to each other and when I select an option in first link box, just show its related data in second

 

please help me

thanks

 


<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Select Cities</title>

</head>


<body>
<table>
<tr>
<td colspan="2">Religion</TD>
<td>
<select name="items">
<option value="">Choose province</option>
<?
$conn=mysql_connect("127.0.0.1", "university", "123456") ;
mysql_select_db("university",$conn) or die ("error cannot connect");

$sql = "SELECT * FROM province";
$result = mysql_query($sql,$conn) or die(mysql_error());
while ($array = mysql_fetch_array($result)) {
$itemName = $array['provincename'];
$itemcode = $array['provinceno']; ?>


<option value="<?php print($itemcode); ?>"><?=$itemName?>

<?
$sqlt = "SELECT * FROM city
LEFT JOIN province ON city.provinceno = province.provinceno
WHERE `province`.`provinceno` = `city`.`provinceno` AND city.provinceno='$itemcode';
";
?>

<?
}  ?>
</select>
</td>
</tr>
<tr>
<td>
<select name="items2">
<option value="">Choose city</option>
<?
$resultt = mysql_query($sqlt,$conn) or die(mysql_error());
while ($array = mysql_fetch_array($resultt)) {
$itemNamet = $array['cityname'];
$itemci = $array['provinceno'];
?>

<option value="<?php print($itemci); ?>"><?=$itemNamet?>
<?
}  ?>


</select>

</td></tr>
</table>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/265997-relation-two-listbox/
Share on other sites

Barand's solution is the correct one.

 

This is more of a client-side problem than a PHP one.

 

but I have to read data from database, let me describe more

in database: I have two tables name "province" & "city"

 

in "province table": proviceno, provincename

 

in "city table": proviceno, cityno, cityname

 

now I have two list box: first list box link to province table in my database, when I click on one of data in list box, second list box show data from city table in database that its proviceno equals to provinceno in "province table"

what should I do? please help me

We understand what you're trying to do. Since you haven't attempted it, and don't want to use a pre-made script, we're not sure how to help you besides providing you with the solution you need, or writing you a personal tutorial.

 

I write code at first topic but the problem is: when I choose a province in first list box at second list box city doesnt change to its relative data

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.