Page 1 of 1

PHP/MYSQLI retain dropdown list values in two dropdown lists

Posted: Thu May 12, 2016 11:58 pm
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";
	}
?>

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

Posted: Fri May 13, 2016 6:05 am
by Celauran
Doesn't look like you're loading anything into the industry list on page load, you're only loading sectors.

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

Posted: Fri May 13, 2016 11:55 am
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!