cedtech31 Posted March 24, 2007 Share Posted March 24, 2007 I am trying to use PHP to save entry on a user input in a form, so that if the user fails the form validation he/she does not have to fill in the information they alread typed in the form the first time. I realize that I can use $_POST['key'] for input fields but I am have trouble with saving select drop down menu input. I figured I would just create a blank option tag and place $_POST['key'] in between the brackets. It works but I know there is a better more robust way to solve this issue. code below... <label for="dept">Department: *</label> <select name="dept" id="dept"> <option>$_POST['dept']</option> <option>Accounting</option> <option>Billing</option> <option>Clinical</option> <option>CR&D</option> <option>Dental</option> <option>Finance</option> <option>FrontDesk</option> <option>Laboratory</option> <option>Medical Records</option> <option>IS</option> </select> I did some research and I read about a technique where I would do something like the code below <label for="dept">Department: *</label> <select name="dept" id="dept"> <option></option> <option <?php if($_POST['dept'] == 'Accounting'){ echo 'selected="selected"'; } ?> >Accounting</option> <option <?php if($_POST['dept'] == 'Billing'){ echo 'selected="selected"'; } ?>>Billing</option> <option>Clinical</option> <option>CR&D</option> <option>Dental</option> <option>Finance</option> <option>FrontDesk</option> <option>Laboratory</option> <option>Medical Records</option> <option>IS</option> </select> as you can see this can lead to a lot of extra lines of code. Does anyone know of a better more robust way to save the user input in a form? maybe using an array Link to comment https://forums.phpfreaks.com/topic/44091-solved-save-select-field-input/ Share on other sites More sharing options...
DanDaBeginner Posted March 24, 2007 Share Posted March 24, 2007 yeah that will work.. but I suggest to lessen the codes, array the content of your select, then loop it upon looping put an the if statement.. something like this: $x = count($dept); for ($i=0; $i<=$x; $i++) { <option if($_POST['dept'] == $dept[$i]){ echo 'selected="selected"'; } ?> >$dept[$i]</option> } this is just an example Link to comment https://forums.phpfreaks.com/topic/44091-solved-save-select-field-input/#findComment-214098 Share on other sites More sharing options...
cedtech31 Posted March 24, 2007 Author Share Posted March 24, 2007 I tried the code below but I am having trouble with my statement in the foreach statement. Any ideas? <label for="dept">Department: *</label> <select name="dept" id="dept"> <?php $dept = array('Billing', 'Clinical', 'CR&D', 'Dental', 'Finance', 'FrontDesk', 'Laboratory', 'Medical Records', 'IS'); foreach($dept as $name) { <option if($_POST['dept'] == $name){ selected="selected" > $name </option> } ?> </select> Link to comment https://forums.phpfreaks.com/topic/44091-solved-save-select-field-input/#findComment-214104 Share on other sites More sharing options...
DanDaBeginner Posted March 24, 2007 Share Posted March 24, 2007 dont use foreach use for or anything else... Link to comment https://forums.phpfreaks.com/topic/44091-solved-save-select-field-input/#findComment-214109 Share on other sites More sharing options...
cedtech31 Posted March 24, 2007 Author Share Posted March 24, 2007 <label for="dept">Department: *</label> <select name="dept" id="dept"> <?php $dept = array('Billing', 'Clinical', 'CR&D', 'Dental', 'Finance', 'FrontDesk', 'Laboratory', 'Medical Records', 'IS'); foreach($dept as $name) { $selected = ''; if ($_POST['dept'] == $name){ $selected = ' selected'; } echo '<option value="'.$name.'"'.$selected.">{$name}</option>\n"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/44091-solved-save-select-field-input/#findComment-214210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.