DaveMag Posted April 18, 2021 Share Posted April 18, 2021 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/312488-dropdown-populating-from-another-dropdown/ Share on other sites More sharing options...
Barand Posted April 18, 2021 Share Posted April 18, 2021 I use AJAX for this task Quote Link to comment https://forums.phpfreaks.com/topic/312488-dropdown-populating-from-another-dropdown/#findComment-1585913 Share on other sites More sharing options...
MadTechie Posted April 18, 2021 Share Posted April 18, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312488-dropdown-populating-from-another-dropdown/#findComment-1585915 Share on other sites More sharing options...
DaveMag Posted April 19, 2021 Author Share Posted April 19, 2021 Thanks for tip on pasting code. First time using this forum. No errors at all. The capitalization in the table name is correct. The query for the first dropdown getting the state names is correct. Quote Link to comment https://forums.phpfreaks.com/topic/312488-dropdown-populating-from-another-dropdown/#findComment-1585926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.