Guys I am trying to create a data editing form. For example I will enter "email address" and script will get record of that email address from database and show values in fields for edition. The problem is one field is drop down list field like I wish that script automatically select that value which is taken from database not always show FIRST value "WORKING". Above is just example otherwise I have another field that has 6 options. Please help me. GCS
<option>Working</option><option selected>Faulty</option></select> Code (markup): Option Faulty will show as selected
But what if value stored in database is "Working", then at the time of editing script should show "Working" by default. In your way "Faulty" will be selected/shown permanently no matter what value is in database. ? GCS
If I understand correctly, then all you need to do is compare the current dbase value with each SELECT value as you generate them, and also echo "selected" if they are the same. You SHOULD be producing select dropdowns in PHP with a loop, I guess, so something like this (taken from some old code of mine, never mind about the rest of it, it's more complicated than what you need! Just look at the bit that generates the HTML at the end): for ($i = 0; $i < sizeof($locals); $i++){ $menuPID = $locals[$i][3]; if ($menuPID) { echo "<option value='" . $menuPID . "'"; if ( $WBOptions['placement_id'] == $menuPID ){ echo "selected"; } echo ">" . $CountryName . "</option>";} } ?> Code (markup): The code echoes "<option value="... bla bla for each option and if my current "placement_id'" (which is my currently selected value) is equal to the current ID in the loop (which is the current dbase value, stored in the variable $menuPID), then "selected" is also echoed. And that's it. Was that what you were trying to do?
If my <select> looked like: <select> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> HTML: And the code that output that was: $array = array( "1" => "One", "2" => "Two", "3" => "Three" ); function create_select($input) { echo "<select>" . "\n"; foreach($input as $value => $option) { echo "<option value='$value'>$option</option>" . "\n"; } echo "</select>"; } PHP: I'd edit create_select() to take a second parameter, like so: function create_select($input, $selected) { echo "<select>" . "\n"; foreach($input as $value => $option) { if ($selected == $value) { $str = " selected='selected'"; } else { $str = ""; }; echo "<option ".$str."value='$value'>$option</option>" . "\n"; } echo "</select>"; } create_select($array, 2); // Would create the selector with 2/Two "selected" PHP:
Right, Wogan, similar approach to me. In fact, can't think how else you WOULD do it Though when I was a relative PHP n00b I'm sure I did it some nasty way!
this is the logic $result // from mysql query $options // array for options, can be fecth from query too $out .= '<select>'; foreach($options as $option) { if($option->value == $result->value) { $out .= '<option value="'.$option->key.'" selected="selected">'.$option->value.'</option>'; } else { $out .= '<option value="'.$option->key.'">'.$option->value.'</option>'; } } $out .= '</select>'; PHP: that just logic regards
Hi guys, Thanks for everybody for replying and helping me but I am sorry to say that I am confused. I have to options "Working" and "Faulty", if you can type any code for this, I will be thankful. Here is input Select Options: Working, Faulty Database Retrieved Variable: $dbselect Input field name: $emailstatus In reference to above information, if you can give me any code that I apply to my php file I will be thankful. GCS