PHP/MYSQLI retain dropdown list values in two dropdown lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP/MYSQLI retain dropdown list values in two dropdown lists

Post by cjkeane »

Hi.
I have two chained dropdown lists almost working. There is an issue with the second dropdown list remembering the choice saved in the db. Choosing an option from the first dropdown list filters the second. The first dropdown list choice and the second dropdown list choice are saved in the db. The first dropdown list retains the selection when the page is loaded/saved again, but the second does not. Below is my coding. Can anyone suggest a solution? I'm almost there, i just need the second dropdown list to return/display the selection saved in the db.

my ajax call:

Code: Select all


<script type="text/javascript">
    $(document).ready(function() {
         $("#sector").change(function()  {
             var sector=$("#sector").val();
                   $.ajax({
                        type:"post",
                        url:"getindustry.php",
                        data:"sector="+sector,
                        success:function(data) {
                            $("#industry").html(data);
                        }
                   });
              });
        });
 </script>
my main php file:

Code: Select all


<?php
     $sector = $_POST['sector'];
     $query_sector = mysqli_query($dbc,"SELECT sector FROM sectors ORDER BY sector");
     while($row_sector=mysqli_fetch_array($query_sector))	{
	    $sector_selected = ($row_sector['sector']==$sector) ? ' selected="selected"' : '';
             $sector_options .= "<option value=\"{$row_sector['sector']}\"{$sector_selected}>{$row_sector['sector']}</option>\n"; 	  
      }
?>
<select name="sector" id="sector" >
    <option value="">< select a sector > <?php echo $sector_options ?></option>
</select>
<select name="industry" id="industry">
    <option disabled>Select Industry</option>
</select>
my getindustry.php file:

Code: Select all

<?php
    ob_start();
    session_start();
    include ('database_connection.php');
    if(!isset($_SESSION['Email'])){
        $page ="index.php";
         header("Refresh: 0; url=$page"); 
         echo " "; 
    }
    $loggedin = $_SESSION['Email'];
    $industry_sql = "SELECT industry FROM companies WHERE contact_email='$loggedin'";
    $industry_result=mysqli_query($dbc,$industry_sql);
    $industry_row=mysqli_fetch_assoc($industry_result);
    if($industry_row) {$industry = $industry_row['industry'];}
?>
<option disabled>Select industry</option>
<?php
    $sector = $_POST['sector'];
    $industry_query = mysqli_query($dbc,"select industry from industries where sector ='$sector' order by sector"); 
    while($row_industry=mysqli_fetch_array($industry_query)){
	$industry_selected = ($industry_row['industry']==$industry) ? ' selected="selected"' : '';
	echo "<option value=\"{$industry_row['industry']}\"{industry_selected}>{$industry_row['industry']}</option>\n";
	}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP/MYSQLI retain dropdown list values in two dropdown l

Post by Celauran »

Doesn't look like you're loading anything into the industry list on page load, you're only loading sectors.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQLI retain dropdown list values in two dropdown l

Post by cjkeane »

Celauran wrote:Doesn't look like you're loading anything into the industry list on page load, you're only loading sectors.
yes i realized that and i found the issue and rectified it. thanks!
Post Reply