Jump to content

[SOLVED] Save Select field input


cedtech31

Recommended Posts

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

:) 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

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>

<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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.