samuel_lopez Posted November 23, 2015 Share Posted November 23, 2015 Hi to all. How can I retain select box value after submitthis is my current code <?php$query = $mysqli->query("Select Proj_ID as id, PROJECT_NAME as project from tblproject"); ?><select name="project" class="required" id="selproject"><option disabled selected>Select Project</option><?php while($option = $query->fetch_object()){ ?><option value="<?php echo $option->id; ?>"><?php echo $option->project; ?></option><?php } ?></select> Thank you Quote Link to comment https://forums.phpfreaks.com/topic/299560-how-to-retain-select-box-value-after-submit/ Share on other sites More sharing options...
crazycoder Posted November 23, 2015 Share Posted November 23, 2015 You will have to return the selected value through GET parameter, and if it has value select it using conditional statement. Quote Link to comment https://forums.phpfreaks.com/topic/299560-how-to-retain-select-box-value-after-submit/#findComment-1526976 Share on other sites More sharing options...
benanamen Posted November 23, 2015 Share Posted November 23, 2015 (edited) You will have to return the selected value through GET parameter, If the form is POST'd as most forms are, the value will be in POST, not GET. Edited November 23, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/299560-how-to-retain-select-box-value-after-submit/#findComment-1526977 Share on other sites More sharing options...
Jacques1 Posted November 23, 2015 Share Posted November 23, 2015 In any case: While you iterate over your rows, you need to compare $option->id with the previously submitted value, be it $_POST['project'] or $_GET['project']. If the current option ID matches the submitted value, you emit a selected attribute. Of course you also need to remove the dummy option in case there's a preselected option, so you have to iterate over the rows before you assemble the HTML markup for the select element. Quote Link to comment https://forums.phpfreaks.com/topic/299560-how-to-retain-select-box-value-after-submit/#findComment-1526980 Share on other sites More sharing options...
Psycho Posted November 23, 2015 Share Posted November 23, 2015 (edited) I prefer to create a function to generate my select fields. I would also highly suggest you follow a standard of putting your logic (i.e. PHP code) at the top of the document and only output the generated content within the HTML at the bottom of the page. Here is a quick example <?php //Function to create select options function createSelectOptions($optionList, $selectedValue=NULL) { $optionsHTML = ''; foreach($optionList as $value => $label) { $selected = ($value == $selectedValue) ? " selected = 'selected'" : ''; $optionsHTML .= "option value=\"{$value}\"{$selected}>{$label}</option>\n"; } return $optionsHTML; } //Get previously selected value (if passed) $projectSelected = isset($_POST['project']) ? $_POST['project'] : NULL; //Query list of available values $projectList = array(); $query = "Select Proj_ID as value, PROJECT_NAME as label from tblproject"; $result = $mysqli->query($query); while($option = $result->fetch_object()) { $projectList[$option->value] = $option->label; } //Create the options for the project select list $projectOptions = createSelectOptions($projectList, $projectSelected); ?> <html> <head></head> <body> <select name="project" class="required" id="selproject"> <?php echo $projectOptions; ?> </select> </body> </html> Edited November 23, 2015 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/299560-how-to-retain-select-box-value-after-submit/#findComment-1527003 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.