jonnyuk3 Posted February 23, 2012 Share Posted February 23, 2012 Hi all, I have a listbox on a form that gets it's data from a MySQL query, all of which is working fine. I make a selection in my listbox and then submit the form which is reloading itself. However on the reload I would like the listbox to display the selection made by the user, instead it reverts to the first item in the list. I have included the code, if someone could point me in the right direction that would be great. I have searched these forums and tried a few things that are suggested but I still can't get it to work. <?php include("dbconn.php"); $query = "SELECT location.location_name, printer.model_number, location_printer.serial_number" . " FROM location, printer, location_printer" . " WHERE location.location_id = location_printer.location_id" . " AND printer.printer_id = location_printer.printer_id"; $result = mysql_query($query); echo "<table>"; echo "<th>Location</th>"; echo "<th>Printer</th>"; echo "<th>Serial No</th>"; while(($row = mysql_fetch_array($result))) { echo "<tr>"; echo "<td>".$row['location_name']."</td>"; echo "<td>".$row['model_number']."</td>"; echo "<td>".$row['serial_number']."</td>"; echo "</tr>"; } echo "</table>"; if(isset($_POST['location'])) { $loc = $_POST['location']; echo "<p>Location = <b> $loc </b>"; } ?> <html> <head> <title>Loop Results</title> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <table> <tr> <td> <?php $query = "SELECT * FROM location ORDER BY location_name"; $result = mysql_query($query); $default = $_POST['location']; echo "<p>Location: "; echo "<select name='location'>"; while(($row = mysql_fetch_array($result))) { echo "<option value='{$row['location_id']}"; //Selected value if($row['location_id'] == $default) echo " selected "; echo "'>{$row['location_name']}</option>\n"; } echo "</select>"; ?> </td> </tr> <tr> <td> <input type="submit" /> </td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/ Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 add if(isset($_POST['location']) && $row['location_id'] == $_POST['location']) echo " selected "; this working of the form is submitted to its self for further updates... Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/#findComment-1320379 Share on other sites More sharing options...
jonnyuk3 Posted February 23, 2012 Author Share Posted February 23, 2012 hi blacknight, thanks for the reply. yes, the form will be expanded later. I just need to get this working first. I tried what you suggested and it still doesn't work!! This is where I added the code, is this correct?? echo "<p>Location: "; echo "<select name='location'>"; while(($row = mysql_fetch_array($result))) { echo "<option value='{$row['location_id']}"; //Default value if(isset($_POST['location']) && $row['location_id'] == $_POST['location']) echo " selected "; echo "'>{$row['location_name']}</option>\n"; } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/#findComment-1320382 Share on other sites More sharing options...
Adam Posted February 23, 2012 Share Posted February 23, 2012 You have the selected attribute within the value attribute. Close that off within the first echo statement, and remove the single quote from the third. Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/#findComment-1320384 Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 yea basicly i see that now ... try this.. echo "<option value='{$row['location_id']}'"; //Default value if(isset($_POST['location']) && $row['location_id'] == $_POST['location']) echo " selected "; echo ">{$row['location_name']}</option>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/#findComment-1320387 Share on other sites More sharing options...
jonnyuk3 Posted February 23, 2012 Author Share Posted February 23, 2012 Thanks guys, I've been stuck on that all morning!! Quote Link to comment https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/#findComment-1320400 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.