Page 1 of 1

How to display fields using the select tag

Posted: Thu Nov 06, 2008 1:53 pm
by jwcrosby
I have a form that writes to and draws data from an MySQL database. For one of the fields I use a drop-down menu (select tag) from which the user selects one of three choices.

Writing the selection to the database is working fine. However, when the user re-opens the form, I want those select boxes to show the previously selected item (i.e., what is now stored in the database). How do I write that code?

Here's the select statement to work from (this is without any attempt to show any existing data from the database):

<td class="fieldcell"> <select name="relationship_depth" id="relationship_depth" tabindex="3">
<option value="">-- Select --</option>
<option value="Very Well">Very Well</option>
<option value="Well">Well</option>
<option value="Casually">Casually</option>
<option value="Hardly">Hardly</option>
</select></td>

Thanks for any suggestions.

Jerry

Re: How to display fields using the select tag

Posted: Thu Nov 06, 2008 3:27 pm
by andyhoneycutt
Well, since you are matching this data against something in the database already, I would suggest pulling the data for the select list options from the database and building it dynamically. When you are looping through your data, compare the data you are pulling against the data (in whichever table it may reside) that keeps track of the user's selection. If they are the same, write "selected=true" to it, otherwise write nothing.

I hope I didn't miss what you're asking, but if so please do elaborate more and I'll try to better answer your question.

-Andy

Re: How to display fields using the select tag

Posted: Thu Nov 06, 2008 3:53 pm
by jwcrosby
I think I understand what you are suggesting, Andy. Sounds like I would put the five options (-Select-, Very Well, Well, Casually, Hardly) in rows in a separate table, from which I could loop the contents of the drop-down list. Am I even close?

I will take me awhile to wrap my mind around this...I'm pretty green.

Jerry

Re: How to display fields using the select tag

Posted: Wed Nov 12, 2008 6:36 am
by amit007
You can try this code:

<td class="fieldcell"> <select name="relationship_depth" id="relationship_depth" tabindex="3">
<option value="" >-- Select --</option>
<option value="Very Well" <?php if(Very Well=="value from databse") {?> echo selected="selected"<?php }?>>Very Well</option>
<option value="Well" <?php if(Well=="value from databse") {?> echo selected="selected"<?php }?>>Well</option>
<option value="Casually" <?php if(Casually=="value from databse") {?> echo selected="selected"<?php }?>>Casually</option>
<option value="Hardly" <?php if(Hardly=="value from databse") {?> echo selected="selected"<?php }?>>Hardly</option>
</select></td>

Re: How to display fields using the select tag

Posted: Wed Nov 19, 2008 10:42 am
by andyhoneycutt
jwcrosby wrote:I think I understand what you are suggesting, Andy. Sounds like I would put the five options (-Select-, Very Well, Well, Casually, Hardly) in rows in a separate table, from which I could loop the contents of the drop-down list. Am I even close?

I will take me awhile to wrap my mind around this...I'm pretty green.

Jerry
Yes, that's basically all you're doing here. If you have a table:

Code: Select all

CREATE TABLE my_options (
option_text varchar(25),
option_selected bool
);
And say it's populated like this:

Code: Select all

"Very Well", 0
"Well", 0
"Casually", 1
"Hardly", 0
You can see that "Casually" is selected. So, in your php you are able to do something like this:

Code: Select all

...
while( $row = mysql_fetch_assoc($result_set) )
{
  // Check to see if our current row is the one that the user has selected:
  $selected_html = "";
  if( $row['option_selected'] == 1 )
  {
    $selected_html = "selected=\"selected\"";
  }
  echo "<option value=\"{$row['option_text']}\" $selected_html>{$row['option_text']}</option>\n";
}
-Andy

Re: How to display fields using the select tag

Posted: Tue Nov 25, 2008 10:51 am
by jwcrosby
amit007 wrote:You can try this code:

<td class="fieldcell"> <select name="relationship_depth" id="relationship_depth" tabindex="3">
<option value="" >-- Select --</option>
<option value="Very Well" <?php if(Very Well=="value from databse") {?> echo selected="selected"<?php }?>>Very Well</option>
<option value="Well" <?php if(Well=="value from databse") {?> echo selected="selected"<?php }?>>Well</option>
<option value="Casually" <?php if(Casually=="value from databse") {?> echo selected="selected"<?php }?>>Casually</option>
<option value="Hardly" <?php if(Hardly=="value from databse") {?> echo selected="selected"<?php }?>>Hardly</option>
</select></td>
Actually, your code didn't work. I worked with it and finally got it to work with some changes. Here's what WILL work ($RelDepth is the name of the variable field from the database.):

<form id="form1" name="form1" method="post" action="">
<select name="select">
<option value="">--Select--</option>
<option value="Very Well" <? if($RelDepth == "Very Well") {echo "selected='selected'";}?>>Very Well</option>
<option value="Casually" <? if($RelDepth == "Casually") {echo "selected='selected'";}?>>Casually</option>
<option value="Well"<? if($RelDepth == "Well") {echo "selected='selected'";}?>>Well</option>
<option value="Hardly"<? if($RelDepth == "Hardly") {echo "selected='selected'";}?>>Hardly</option>
</select>
</form>
Jerry