Drop down Problem

Discussion in 'PHP' started by presha, Apr 1, 2012.

  1. #1
    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>
     
    presha, Apr 1, 2012 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    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?
     
    samyak, Apr 1, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    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:
     
    Alex Roxon, Apr 1, 2012 IP
  4. Artuurs

    Artuurs Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There is much of SQL injections ... ;)
     
    Artuurs, Apr 7, 2012 IP