Jump to content

How to retain Select option after submitting the form in php


Go to solution Solved by Ch0cu3r,

Recommended Posts

I looked at several community forums, and I am unable to figure it out on how to retain select option value after validation fails.

Here is the code that works for me, but values disappear when submit button is submitted.

<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice
= mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>

<?php
}
?>
</select>

Here is what I tried and doesn't work for me:

<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice
= mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>

<?php
}
?>
</select>

Hi, 

 

I'd do something like:

<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
if (isset($_POST('service'))){
		echo "<option value='" . $_POST('service') . "' selected='selected'>'" . $_POST('service') . "'</option>";
	}

while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>

<?php
}
?>

Hope this helps...

I am not liking this:

<option value="<?php
  echo $line['serviceid'];
  if ($_POST['service'] == $service) { // What value does $service currently hold?
    echo 'selected="selected"';
  }
  echo $line['serviceid'];
?>"> <?php
  echo $line['service'];
?> </option>

In the code above, the variable $service will need to equal the value being held in $_POST['service'] for a true result. Let us know if it is defined anywhere.

 

The code is iterating through the $resultservice array on a $line by $line basis. So, $_POST[service'] will actually need to equal the current iteration's $line['serviceid'] for a true result.

Edited by bsmither

Hello bsmither,

 

Thank you for your quick response. Once the form is submitted, I am holding that value in below code:

<?php

                if (isset($_POST['submit'])) {

                if (empty($_POST['service'])) {

                echo "Please select service in dropdown" . "</br>";

                }

                else {

                $service = $_POST['service'];

                }

I hope that answers your question. Do you want me to copy paste entire code?

 

Thanks,

 

Ray

Ok, so we have this at some point:

$service = $_POST['service'];

 

Then we have this inside the loop -- looking for something that changes in each iteration of the loop:

if ($_POST['service'] == $service) {

 

Consider that $_POST['service'] and $service never change at any point in the loop, there is nothing different that is being checked.

 

The code needs to test on that thing which changes for every iteration of the loop: $line['serviceid']

if ($line['serviceid'] == $service) {

Ahh I see what you mean... I have to replace $_POST['service'] == $service to ($line['serviceid'] == $service)

 

so it would look like this:

<option value="<?php echo $line['serviceid']; if ($line['serviceid'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option>

I will test this and keep you posted :)

 

Thank you very much.

 

Ray

Please be very careful and note where the quote marks are located:

<option value="<?php echo $line['serviceid']; if ($line['serviceid'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?>">

 

The above line, when completed, will look like:

<option value="3selected="selected"3">

 

Your statement should read:

<option value="<?php echo $line['serviceid']; ?>" <?php if ($line['serviceid'] == $service) {echo 'selected="selected"'} ?>>

 

The above line gives, when completed:

<option value="3" selected="selected">

Edited by bsmither

Hello bsmither, 

 

Thank you for your advice. I really appreciate your help on educating me. I was stuck on post #6 and couldn't figure out how to fix the broken html page. However, the broken html page is still not fixed after making the change. But when I remove the code and keep this original code I am able to browse the page without any issue. 

 

Here is the original code which works:

    	<?php
        include('dbcon.php');
		
		?>
        <form id="searchform" action="index.php" method="post">
    	<table>
  		<tr>
    		  <td class="searchleftcol"><h3>Service:</h3></td>
    		<td>
                 <select id="service" name="service" class="searchoption">
                 <option value="">-- Select Service Name --</option>
                 <?php
                 $resultservice = mysqli_query($con,"Select * from services") ?>
                 <?php

		 while ($line = mysqli_fetch_array($resultservice)) {
                 ?>
       		<option value="<?php echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option> 
                <?php
                }
                ?>
                </select>
            
            </td>
                  </tr>
                  <tr>
                    <td>
                        <h3>Environment:</h3>
                    </td>
                    <td>
                        <select id="environment" name="environment" class="searchoption">
                        <option value="">-- Select Environment --</option>
                        <?php
                        $resultdomain = mysqli_query($con,"Select * from evn") ?>
                        <?php
                        while ($line = mysqli_fetch_array($resultdomain)) {
                        ?>
                                        
                        <option value="<?php echo $line['envid'];?>"> <?php echo $line['env'];?> </option>
                                        
                        <?php
                        }
                        ?>
                        </select>    	
                    
                    </td>
                  </tr>
                  <tr>
                    <td>
                        <h3>Status:</h3>
                    </td>
                    <td>
                        <select name="status" class="searchoption">
                        <option value="Active">Active</option>
                        <option value="Inactive">Inactive</option>
                        </select>
                    
                    </td>
                  </tr>
                </table>
                            <input type="reset" name="reset">
                            <input type="submit" name="submit" value="Search">
                            
                            </ul>
		</form>
        
    
        <?php
		
		if (isset($_POST['submit'])) {

		if (empty($_POST['service'])) {

        	echo "Please select service in dropdown" . "</br>";

		}

		else {

       		$service = $_POST['service'];

		}
		
		if (empty($_POST['environment'])) {
			
			echo "Please select Environment in dropdown" . "</br>";	

		}
		
		else {
			
			$env = $_POST['environment'];
			
		}
		
		if ((!empty($service)) && (!empty($env))) { 
		
		$sql="select * from TableName"; //Original Query removed

		if (!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
        }
		
		$mydata = mysqli_query($con,$sql);
		$rowcount = mysqli_num_rows($mydata);

		if ($rowcount >= 1) {
		echo "<table id='main-data-table' border=1>
		  <tr>
			<th> + </th>
			<th>Server Name</th>
			<th>IP Address</th>
			<th>Service</th>
			<th>Tier</th>
		  </tr>";
		while ($record = mysqli_fetch_array($mydata)) {
		echo "<tr class='main mainrow'>";
		echo "<td><a href='#' class='main'>+</a></td>";
		echo "<td>" . $record['ServerName'] . "</td>";
		echo "<td>" . $record['IPAddress'] . "</td>";
		echo "<td>" . $record['service'] . "</td>";
		echo "<td>" . $record['tier'] . "</td>";
		echo "</tr>";
		
		//echo "<table border=1>";
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Server . Name . "</td>";
		echo "<td>" . $record['ServerName'] . "</td>";
		echo "<td class='innerleftcol'>" . IP . Address . "</td>";
		echo "<td>" . $record['IPAddress'] . "</td>";
		echo "</tr>";
		
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Alias . "</td>";
		echo "<td>" . $record['alias'] . "</td>";
		echo "<td class='innerleftcol'>" . Environment . "</td>";
		echo "<td>" . $record['env'] . "</td>";
		echo "</tr>";
		
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . VIP . Address . "</td>";
		echo "<td>" . $record['vipaddress'] . "</td>";
		echo "<td class='innerleftcol'>" . Domain . Name . "</td>";
		echo "<td>" . $record['domainname'] . "</td>";
		echo "</tr>";

		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Service . "</td>";
		echo "<td>" . $record['service'] . "</td>";
		echo "<td class='innerleftcol'>" . Tier . "</td>";
		echo "<td>" . $record['tier'] . "</td>";
		echo "</tr>";
				
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . OS . "</td>";
		echo "<td>" . $record['os'] . "</td>";
		echo "<td class='innerleftcol'>" . EndPoint . "</td>";
		echo "<td>" . $record['url'] . "</td>";
		echo "</tr>";

		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Platform . "</td>";
		echo "<td>" . $record['platform'] . "</td>";
		echo "<td class='innerleftcol'>" . Virtual . "</td>";
		echo "<td>" . $record['virtualenv'] . "</td>";
		echo "</tr>";
		}
		echo "</table>";
		echo "$rowcount record/s found";	
		}
		else {
		
			echo "No records found";	
			
		}
		
		}
		else {
			
			exit();	
			
		}
		
		mysqli_close($con);
		}
		
		?>

</body>
</html>

Hi,

 

Try this (CODE NOT TESTED)

    	<?php
        include('dbcon.php');
		
		?>
        <form id="searchform" action="index.php" method="post">
    	<table>
  		<tr>
    		  <td class="searchleftcol"><h3>Service:</h3></td>
    		<td>
                 <select id="service" name="service" class="searchoption">
                 <option value="">-- Select Service Name --</option>
                
                 <?php
                 if (isset($_POST['service'])) {
                 $serviceID = $_POST['service'];
                 $resultservice = mysqli_query($con,"Select * from services WHERE ID=$serviceID") ?>
                 <?php

		 			while ($line = mysqli_fetch_array($resultservice)) {
                 ?>
       				<option value="<?php echo $line['serviceid']; ?>" selected="selected"> <?php echo $line['service'];?> </option> 
                <?php
                }
                }
                ?>
                
                 <?php
                 $resultservice = mysqli_query($con,"Select * from services") ?>
                 <?php

		 			while ($line = mysqli_fetch_array($resultservice)) {
                 ?>
       				<option value="<?php echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option> 
                <?php
                }
                ?>
                </select>
            
            </td>
                  </tr>
                  <tr>
                    <td>
                        <h3>Environment:</h3>
                    </td>
                    <td>
                        <select id="environment" name="environment" class="searchoption">
                        <option value="">-- Select Environment --</option>
                        <?php
                        if (isset($_POST['environment'])){
                        $eviID = $_POST['environment'];
                        $resultdomain = mysqli_query($con,"Select * from evn WHERE ID=$eviID") ?>
                        <?php
                        while ($line = mysqli_fetch_array($resultdomain)) {
                        ?>
                                        
                        <option value="<?php echo $line['envid'];?>" selected="selected"> <?php echo $line['env'];?> </option>
                                        
                        <?php
                        }}
                        ?>
                        
                        <?php
                        $resultdomain = mysqli_query($con,"Select * from evn") ?>
                        <?php
                        while ($line = mysqli_fetch_array($resultdomain)) {
                        ?>
                                        
                        <option value="<?php echo $line['envid'];?>"> <?php echo $line['env'];?> </option>
                                        
                        <?php
                        }
                        ?>
                        </select>    	
                    
                    </td>
                  </tr>
                  <tr>
                    <td>
                        <h3>Status:</h3>
                    </td>
                    <td>
                        <select name="status" class="searchoption">
                        <option value="Active">Active</option>
                        <option value="Inactive">Inactive</option>
                        </select>
                    
                    </td>
                  </tr>
                </table>
                            <input type="reset" name="reset">
                            <input type="submit" name="submit" value="Search">
                            
                            </ul>
		</form>
        
    
        <?php
		
		if (isset($_POST['submit'])) {

		if (empty($_POST['service'])) {

        	echo "Please select service in dropdown" . "</br>";

		}

		else {

       		$service = $_POST['service'];

		}
		
		if (empty($_POST['environment'])) {
			
			echo "Please select Environment in dropdown" . "</br>";	

		}
		
		else {
			
			$env = $_POST['environment'];
			
		}
		
		if ((!empty($service)) && (!empty($env))) { 
		
		$sql="select * from TableName"; //Original Query removed

		if (!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
        }
		
		$mydata = mysqli_query($con,$sql);
		$rowcount = mysqli_num_rows($mydata);

		if ($rowcount >= 1) {
		echo "<table id='main-data-table' border=1>
		  <tr>
			<th> + </th>
			<th>Server Name</th>
			<th>IP Address</th>
			<th>Service</th>
			<th>Tier</th>
		  </tr>";
		while ($record = mysqli_fetch_array($mydata)) {
		echo "<tr class='main mainrow'>";
		echo "<td><a href='#' class='main'>+</a></td>";
		echo "<td>" . $record['ServerName'] . "</td>";
		echo "<td>" . $record['IPAddress'] . "</td>";
		echo "<td>" . $record['service'] . "</td>";
		echo "<td>" . $record['tier'] . "</td>";
		echo "</tr>";
		
		//echo "<table border=1>";
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Server . Name . "</td>";
		echo "<td>" . $record['ServerName'] . "</td>";
		echo "<td class='innerleftcol'>" . IP . Address . "</td>";
		echo "<td>" . $record['IPAddress'] . "</td>";
		echo "</tr>";
		
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Alias . "</td>";
		echo "<td>" . $record['alias'] . "</td>";
		echo "<td class='innerleftcol'>" . Environment . "</td>";
		echo "<td>" . $record['env'] . "</td>";
		echo "</tr>";
		
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . VIP . Address . "</td>";
		echo "<td>" . $record['vipaddress'] . "</td>";
		echo "<td class='innerleftcol'>" . Domain . Name . "</td>";
		echo "<td>" . $record['domainname'] . "</td>";
		echo "</tr>";

		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Service . "</td>";
		echo "<td>" . $record['service'] . "</td>";
		echo "<td class='innerleftcol'>" . Tier . "</td>";
		echo "<td>" . $record['tier'] . "</td>";
		echo "</tr>";
				
		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . OS . "</td>";
		echo "<td>" . $record['os'] . "</td>";
		echo "<td class='innerleftcol'>" . EndPoint . "</td>";
		echo "<td>" . $record['url'] . "</td>";
		echo "</tr>";

		echo "<tr class='data showhide'>";
		echo "<td>" . "</td>"; 
		echo "<td class='innerleftcol'>" . Platform . "</td>";
		echo "<td>" . $record['platform'] . "</td>";
		echo "<td class='innerleftcol'>" . Virtual . "</td>";
		echo "<td>" . $record['virtualenv'] . "</td>";
		echo "</tr>";
		}
		echo "</table>";
		echo "$rowcount record/s found";	
		}
		else {
		
			echo "No records found";	
			
		}
		
		}
		else {
			
			exit();	
			
		}
		
		mysqli_close($con);
		}
		
		?>

</body>
</html>
  • Solution

@Ansego I do not understand why you find it is necessary to have a separate query for selecting the chosen options? I feel this makes things more complex.

 

All you need to do is compare the $_POST in the while loop, if the current item matches add the selected attribute to the <option>.

<?php
while ($line = mysqli_fetch_array($resultservice))
{
    $selected = (isset($_POST['service']) && $_POST['service'] == $line['serviceid']) ? ' selected="selected"' : '';
?>
<option<?php echo $selected ?> value="<?php echo $line['serviceid']; ?>"> <?php echo $line['service'];?> </option> 
<?php
}
?>

@Ch0cu3r Your 100% right, I simply did not think of that  :suicide: I sometimes take the long winded way of doing things, my bad. I defiantly will remember this next time I am faced with similar problems in the future. Thanks heaps Ch0cu3r, appreciated. Short, sweet & effective code mate.

Woohoo.....!

 

@Ch0cu3r, you are genius. Your code fixed my problem. :)

 

Just to recap what I learned here, is that I have to compare value in the while loop, if it is true it will assign that value to selected variable. Then we are echoing selected value in option, if that is not true it will move along and use whatever value it gets from while loop. 

 

Thank you so much to all for your involvement in fixing this. I will definitely participate on this community more to learn. :)

 

Thanks,

 

Ray

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.