raymak Posted April 19, 2014 Share Posted April 19, 2014 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") ?><?phpwhile ($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") ?><?phpwhile ($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> Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/ Share on other sites More sharing options...
Ansego Posted April 19, 2014 Share Posted April 19, 2014 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... Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476704 Share on other sites More sharing options...
bsmither Posted April 19, 2014 Share Posted April 19, 2014 (edited) 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 April 19, 2014 by bsmither Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476711 Share on other sites More sharing options...
raymak Posted April 20, 2014 Author Share Posted April 20, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476715 Share on other sites More sharing options...
bsmither Posted April 20, 2014 Share Posted April 20, 2014 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) { Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476716 Share on other sites More sharing options...
raymak Posted April 20, 2014 Author Share Posted April 20, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476717 Share on other sites More sharing options...
bsmither Posted April 20, 2014 Share Posted April 20, 2014 (edited) 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 April 20, 2014 by bsmither Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476720 Share on other sites More sharing options...
raymak Posted April 20, 2014 Author Share Posted April 20, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476723 Share on other sites More sharing options...
Ansego Posted April 20, 2014 Share Posted April 20, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476725 Share on other sites More sharing options...
Ansego Posted April 20, 2014 Share Posted April 20, 2014 (edited) Might need to change the SQL statement to be correct, Change the WHERE ID to WHERE serviceid. Same with Environmental part. Edited April 20, 2014 by Ansego Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476726 Share on other sites More sharing options...
Solution Ch0cu3r Posted April 20, 2014 Solution Share Posted April 20, 2014 @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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476742 Share on other sites More sharing options...
Ansego Posted April 20, 2014 Share Posted April 20, 2014 @Ch0cu3r Your 100% right, I simply did not think of that  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. Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476743 Share on other sites More sharing options...
raymak Posted April 20, 2014 Author Share Posted April 20, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287892-how-to-retain-select-option-after-submitting-the-form-in-php/#findComment-1476791 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.