How to pass value with $_GET to a select/option tag

I need help again with figuring out how to pass data from one page to another while using a hyperlink. I managed to get all the regular input field filled-in with the $_GET global through url but can’t seem to be able to stick it inside two of my select tags:

here is the receiving form:

 <form  enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    
        <input type="hidden" name"MAX_FILE_SIZE" value="32768" />
        
        <label for="employeeactive" class="label" >Employee Active:</label>                
            <input name="employeeactive" type="checkbox" required class="input" id="employeeactive" <?php if (isset($_GET['employeeactive']) && $_GET['employeeactive']== 1) echo "checked";?>/>
    
        <label for"jobtitleid" class="label">Job title</label>
        <select type="text" required name="jobtitleid" class="input" id="jobtitleid"/>
            <option value="select"></option>
            <?php
            $jobtitle = "SELECT * FROM jobtitle";
            $jobresult= mysqli_query($dbc, $jobtitle);
            while($option = mysqli_fetch_array($jobresult)){
            $selectedjobtitleid = (isset($_SESSION['jobtitleid']) && $_SESSION['jobtitleid']); 
            echo '<option class"input" value="' . $option['jobtitleid'] . '"' . $selectedjobtitleid .'>' . $option['jobtitle'] . '</option>'."\r";
            }
            ?>
        </select>
            <label for"employerid" class="label">Employer</label>
        <select type="text" required name="employerid"  class="input" id="employerid"/>
            <option  class="option" value="<?php echo $_GET['employerid']; ?>" ></option>
            <?php
           
            if(isset($_GET['employerid'])){
    	  $employerid = $_GET['employerid'];
    	   }
    	  $employerslq = "SELECT employerid, employer FROM employer WHERE employerid= '$employerid'";
    	  $resemp = mysqli_query($dbc, $employerslq);
    	  while ($resemp2 = mysqli_fetch_array($resemp)){
    	  echo'<option value="' .$resemp2['employerid'].'" >' . $resemp2['employer'] . '</option>'."\r";
    	 }else{ 
    	  $employer = "SELECT employerid,employer FROM employer";
            $empresult= mysqli_query($dbc, $employer);
            while($optionemp = mysqli_fetch_array($empresult)){                    
            $selectedemployerid = (isset($_SESSION['employerid']) && $_SESSION['employerid']); 
    		  echo '<option class="option" value="' . $optionemp['employerid'] . '"' . $selectedemployerid .'>' . $optionemp['employer'] . '</option>'."\r";
    	  }
         
    	  echo $employerid;
            ?>
        </select>
            <label for="employeeid" class="label" >Employee ID:</label>                
            <input name="employeeid" type="text" required class="input" id="employeeid" value="<?php echo $_GET['employeeid']; ?>"/>
            
            <label for="firstname" class="label" >First Name:</label>                
            <input name="firstname" type="text" required class="input" id="firstname" value="<?php echo $_GET['firstname']; ?>"/>
            
            <label for="lastname" class="label" >Last Name:</label>                
            <input name="lastname" type="text" required class="input" id="lastname" value="<?php echo $_GET['lastname']; ?>" />
            
            <label for="address" class="label" >Address:</label>                
            <input type="text" class="input" id="address" name="address" value="<?php echo $_GET['address']; ?>" />
            
            <label for="city" class="label" >City:</label>                
            <input type="text" class="input" id="city" name="city" value="<?php echo $_GET['city']; ?>" />
            
            <label for="province" class="label" >Province:</label>                
            <input type="text" class="input" id="province" name="province" value="<?php echo $_GET['province']; ?>"/>
            
            <label for="country" class="label" >Country:</label>                
            <input type="text" class="input" id="country" name="country" value="<?php echo $_GET['country']; ?>" />
            
            <label for="postalcode" class="label" >Postal Code:</label>                
            <input type="text" class="input" id="postalcode" name="postalcode" value="<?php echo $_GET['postalcode']; ?>"/>
            
            <label for="phone" class="label" >Phone:</label>                
            <input type="tel" class="input" id="phone" name="phone" value="<?php echo $_GET['phone']; ?>"/>
            
            <label for="email" class="label" >Email:</label>
            <input type="email" class="input" id="email" name="email" value="<?php echo $_GET['email']; ?>"/><br /><br />
            
            <label class="label" id="employeecommentlabel" for="employeecomment">Employee Comment:</label><br />                
            <textarea name="employeecomment" class="textarea" type="text"></textarea><br />  
            
            <label class="label" id="employeepiclabel"for="employeepic">Employee Picture:</label>
            <input class="input"type="file" name="employeepic" id="employeepic" />
            <input class="input" id="empsubmit" type="submit" name="submit" value="Save"/>
    </form>

and here is the sending code:

echo'<table class="tblviewemp" >';
		
		echo '<thead>';
			echo '<tr class="tblviewemp">';
			echo'<th class="dataH" id="emppicthumb">Employee Pic </th>';
			echo'<th class="dataH"> First Name</th>';
			echo'<th class="dataH">Last Name </th>';
			echo'<th class="dataH"> ID</th>';
			echo'<th class="dataH"> Update</th>';
		echo '</thead>';
		
		// Display the employee data and link to update
		while ($row = mysqli_fetch_array($result)) {
			
		echo '<tr>';
			//echo '<td class="thumb"><img  src="' . EMP_UPLOADPATH . $row['employeepic'] . '" alt="Employee image" /></td>';
			echo '<td class="data">' . $row['employeeid'] . '</td>';
			echo '<td class="data">' . $row['firstname'] . '</td>';
			echo '<td class="data">' . $row['lastname'] . '</td>';
			echo '<td><a href="update_employee.php?' .'&amp;employeeactive='  . $row['employeeactive'] . 
			'&amp;employeeid='  . $row['employeeid'] . 
			'&amp;employerid='  . $row['jobtitleid'] . 
			'&amp;jobtitleid='  . $row['firstname'] . 
			'&amp;firstname='  . $row['firstname'] . 
			'&amp;lastname='   . $row['lastname'] .
			'&amp;address='    . $row['address'] .
			'&amp;city='       . $row['city'] . 
			'&amp;province='   . $row['province'] .
			'&amp;country='    . $row['country'] .
			'&amp;postalcode=' . $row['postalcode'] .
			'&amp;phone='      . $row['phone'] . 
			'&amp;email='      . $row['email'] .'">update</a></td>';
		}
		echo '</tr>';
	echo'</table>';

my apologies. I sent the old version of the sending code with mistakes in it. I have corrected the field references:

echo'<table class="tblviewemp" >';
		
		echo '<thead>';
			echo '<tr class="tblviewemp">';
			echo'<th class="dataH" id="emppicthumb">Employee Pic </th>';
			echo'<th class="dataH"> First Name</th>';
			echo'<th class="dataH">Last Name </th>';
			echo'<th class="dataH"> ID</th>';
			echo'<th class="dataH"> Update</th>';
		echo '</thead>';
		
		// Display the employee data and link to update
		while ($row = mysqli_fetch_array($result)) {
			
		echo '<tr>';
			//echo '<td class="thumb"><img  src="' . EMP_UPLOADPATH . $row['employeepic'] . '" alt="Employee image" /></td>';
			echo '<td class="data">' . $row['employeeid'] . '</td>';
			echo '<td class="data">' . $row['firstname'] . '</td>';
			echo '<td class="data">' . $row['lastname'] . '</td>';
			echo '<td><a href="update_employee.php?' .
			'&amp;employeeactive='  . $row['employeeactive'] . 
			'&amp;employeeid='  . $row['employeeid'] . 
			'&amp;employerid='  . $row['employerid'] . 
			'&amp;jobtitleid='  . $row['jobtitleid'] . 
			'&amp;firstname='  . $row['firstname'] . 
			'&amp;lastname='   . $row['lastname'] .
			'&amp;address='    . $row['address'] .
			'&amp;city='       . $row['city'] . 
			'&amp;province='   . $row['province'] .
			'&amp;country='    . $row['country'] .
			'&amp;postalcode=' . $row['postalcode'] .
			'&amp;phone='      . $row['phone'] . 
			'&amp;email='      . $row['email'] .'">update</a></td>';
		}
		echo '</tr>';
	echo'</table>';

OK, I figured it out, works like a charm with the following adjustment to the receiving form:

 <?php
        if(isset($_GET['jobtitleid'])){
	  $jobtitleid= $_GET['jobtitleid'];
	  $jobsql= "SELECT jobtitleid, jobtitle FROM jobtitle WHERE jobtitleid='$jobtitleid'";
	  $resjobtitle= mysqli_query($dbc, $jobsql);
	  while($jobres2=mysqli_fetch_array($resjobtitle)){
		  echo'<option value="' .$jobres2['jobtitleid'].'" selected>' . $jobres2['jobtitle'] . '</option>'."\r";
	  }
	  }else{
	  $jobtitle = "SELECT * FROM jobtitle";
        $jobresult= mysqli_query($dbc, $jobtitle);
        while($option = mysqli_fetch_array($jobresult)){
        '<option class"input" value="'. $option['jobtitleid']. '>' . $option['jobtitle'] . '</option>'."\r";
        }
	  }
        ?>
    </select>
        <label for"employerid" class="label">Employer</label>
    <select type="text" required name="employerid"  class="input" id="employerid"/>
        <option  class="option" value="<?php echo $_GET['employerid']; ?>" ></option>
        <?php
       
        if(isset($_GET['employerid'])){
	  $employerid = $_GET['employerid'];
	  $employerslq = "SELECT employerid, employer FROM employer WHERE employerid= '$employerid'";
	  $resemp = mysqli_query($dbc, $employerslq);
	  while ($resemp2 = mysqli_fetch_array($resemp)){
	  echo'<option value="' .$resemp2['employerid'].'" selected>' . $resemp2['employer'] . '</option>'."\r";
	  }
	  }else{
	  $employer = "SELECT employerid,employer FROM employer";
        $empresult= mysqli_query($dbc, $employer);
        while($optionemp = mysqli_fetch_array($empresult)){                    
        $selectedemployerid = (isset($_SESSION['employerid']) && $_SESSION['employerid']); 
		  echo '<option class="option" value="' . $optionemp['employerid'] . '"' . $selectedemployerid .'>' . $optionemp['employer'] . '</option>'."\r";
	  }
	  }

I think you would do better to never allow GET anywhere near your DB and just add “selected” to a value option that matches GET. For example:

<?php
$jobtitle = "SELECT jobtitleid,jobtitle FROM jobtitle";
$jobresult= mysqli_query($dbc, $jobtitle);
while($option = mysqli_fetch_array($jobresult)){  
    $selectedjobtitleid = (isset($_GET['jobtitleid']) && $_GET['jobtitleid'] === $option['jobtitleid'] ? ' selected' : ''); 
    echo '<option class"input" value="'. $option['jobtitleid']. '"' . $selectedjobtitleid . '>' . $option['jobtitle'] . '</option>'."\r";
}
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.