I am stuck with this php code if someone could please point out my mistake in this particular code. I am trying to build a parent and child drop down of country and port. <?php include("connection.php"); if(isset($_GET["country"]) && is_numeric($_GET["country"])) { $region = $_GET["country"]; } if(isset($_GET["port"]) && is_numeric($_GET["port"])) { $region = $_GET["port"]; } ?> <html> <head> <title>Trial</title> <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['trialform']; formObject.submit(); } </script> </head> <body> <form name="trialform" method="get"> <select name="country" onChange="autoSubmit();"> <option value="null"></option> <?php $sql_result=mysql_query("select distinct country from port_data"); for($i=1;$row = mysql_fetch_array($sql_result);$i++){ echo" <option value=\"i\" if($country==i) echo \"selected\">".$row['country']."</option> " }?> </select> <?php if($country != null && is_numeric($country)) { ?> <select name="port" onChange="autoSubmit();"> <option value="null"></option> <?php $sql_result =mysql_query("select p_name from port_data where country=(select country from country where country_id=$country)"); for($i=1;$row = mysql_fetch_array($countries);$i++) { echo ("<option ". ($port == $row["p_id"] ? " selected" : "") . ">$row[p_name]</option>"); } ?> </select> <?php } ?> </form> </body> </html>
What problem are you facing exactly? Is it showing error? One thing I noticed is that you are setting the value of '$region' but not really using it anywhere. To make the port select button you are checking if '$country !=null'. So your port select button will not be created. Is that the problem that you are referring to?
Your problem is here: <?php $sql_result=mysql_query("select distinct country from port_data"); for($i=1;$row = mysql_fetch_array($sql_result);$i++){ echo" <option value=\"i\" if($country==i) echo \"selected\">".$row['country']."</option> " }?> PHP: Try something like: <?php $sql_result=mysql_query("select distinct country from port_data"); foreach (mysql_fetch_array($sql_result) as $row){ echo" <option value=\"i\" if($country==i) echo \"selected\">".$row['country']."</option> " }?> PHP: