Jump to content

Dropdown populating from another dropdown


DaveMag

Recommended Posts

I am not a developer but I can modify code to work for me. The following code works on my test machine (Windows 10, IIS, PHP 7.4) but doesn't work on my website (cPanel, Some version of Linux, PHP 7.4). The two dropdowns are for State and City. You are supposed to be able to select the state and then select a city from that state then bring up a report for craft breweries in the city. When selecting State from the first dropdown, the page refreshes, the URL is correct with the reports.php?cat=<STATE> so $cat is being set, but the first dropdown no longer has the state selected and the second dropdown is populated with All cities and not just one ones from the selected state. Any ides why this is working fine on one machine and not the other?

Selected code from reports.php

<?php
$cat=$_REQUEST['cat'];

?>
<SCRIPT language=JavaScript>
    <!--
    function reload(form)
    {
        var val=form.cat.options[form.cat.options.selectedIndex].value;
        self.location='reports.php?cat=' + val ;
    }
        
</script>


<?Php
    ///////// Getting the State from Mysql table for first list box//////////
    $Cyquer2="SELECT DISTINCT breweries.State, states.state FROM breweries INNER JOIN states ON breweries.State=states.abbr ORDER BY states.state"; 
    $Cyresult2 = mysqli_query($con, $Cyquer2);

    /////// for second drop down list we will check if State is selected else we will display all the cities///// 
    if(isset($cat) and strlen($cat) > 0) 
    {
        $Cyquer="SELECT DISTINCT City FROM breweries where State='" . $cat . "' order by City"; 
    }
    else 
    { 
        $Cyquer="SELECT DISTINCT City FROM breweries order by City"; 
    } 
    
    $Cyresult1 = $con->query($Cyquer); 

    echo "<form method=post action='brewerylistbycity.php'>";
        //////////        Starting of first drop downlist ///////// -->
        echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select State</option>";
            while($Cynoticia2 = mysqli_fetch_array($Cyresult2))
            {
                if($Cynoticia2['State']==@$cat)
                {
                    echo "<option selected value='$Cynoticia2[State]'>$Cynoticia2[state]</option>"."<BR>";

                }
                else
                {
                    echo  "<option value='$Cynoticia2[State]'>$Cynoticia2[state]</option>";
                    
                }
            }
        echo "</select>";
        echo "</br>";

        //////////        Starting of second drop downlist /////////
        echo "</br>";
        echo "<select name='subcat' selected=''><option value=''>Select City</option>";
            while($Cynoticia = $Cyresult1->fetch_assoc()) 
            {
                echo  "<option value='$Cynoticia[City]'>$Cynoticia[City]</option>";
            }
        echo "</select></br>";

        //// End Form /////
        echo "<input type=submit value=Submit>";
    echo "</form>";
?>
 

 

Link to comment
Share on other sites

Hi DaveMag

First off, please use the <> script button for pasting code, it really does help,

Like so

<?php
$cat=$_REQUEST['cat'];

?>
<SCRIPT language=JavaScript>
    <!--
    function reload(form)
    {
        var val=form.cat.options[form.cat.options.selectedIndex].value;
        self.location='reports.php?cat=' + val ;
    }
        
</script>


<?Php
    ///////// Getting the State from Mysql table for first list box//////////
    $Cyquer2="SELECT DISTINCT breweries.State, states.state FROM breweries INNER JOIN states ON breweries.State=states.abbr ORDER BY states.state"; 
    $Cyresult2 = mysqli_query($con, $Cyquer2);

    /////// for second drop down list we will check if State is selected else we will display all the cities///// 
    if(isset($cat) and strlen($cat) > 0) 
    {
        $Cyquer="SELECT DISTINCT City FROM breweries where State='" . $cat . "' order by City"; 
    }
    else 
    { 
        $Cyquer="SELECT DISTINCT City FROM breweries order by City"; 
    } 
    
    $Cyresult1 = $con->query($Cyquer); 

    echo "<form method=post action='brewerylistbycity.php'>";
        //////////        Starting of first drop downlist ///////// -->
        echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select State</option>";
            while($Cynoticia2 = mysqli_fetch_array($Cyresult2))
            {
                if($Cynoticia2['State']==@$cat)
                {
                    echo "<option selected value='$Cynoticia2[State]'>$Cynoticia2[state]</option>"."<BR>";

                }
                else
                {
                    echo  "<option value='$Cynoticia2[State]'>$Cynoticia2[state]</option>";
                    
                }
            }
        echo "</select>";
        echo "</br>";

        //////////        Starting of second drop downlist /////////
        echo "</br>";
        echo "<select name='subcat' selected=''><option value=''>Select City</option>";
            while($Cynoticia = $Cyresult1->fetch_assoc()) 
            {
                echo  "<option value='$Cynoticia[City]'>$Cynoticia[City]</option>";
            }
        echo "</select></br>";

        //// End Form /////
        echo "<input type=submit value=Submit>";
    echo "</form>";
?>

Now you do have some issues  main one would be security (SQL injection) but ignoring that for now.

I'm curious about the following.

1 hour ago, DaveMag said:

The following code works on my test machine (Windows 10, IIS, PHP 7.4) but doesn't work on my website (cPanel, Some version of Linux, PHP 7.4)

This maybe use do to the use of the first caps of State in the breweries table, as on a windows system the case is (for the most part) ignored, but on Linux, it's not.

So my first set would be to check the database and see if states has a cap S, 

are you getting any errors ?

Hope this helps

 

Link to comment
Share on other sites

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.