PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi, I'm working on a site for a graduate class and I'm having one small problem with it. I want to show specific information about records in a database in an HTML form on a page.
This was fairly easy to accomplish to do with text boxes as I just set the value of the box to <?php echo($item)?>
However, I also have a drop down select box with 3 option values and I'm having trouble getting the default option set as what identifies the record in my database.
<select name = "category">
<option value = ""></option>
<option value = "one">1</option>
<option value = "two">2</option>
<option value = "three">3</option>
Is there way to set this so the default option value would change, depending on the database record it is displaying?
Last edited by Benjamin on Mon Jun 29, 2009 11:34 pm, edited 1 time in total.
Reason:Added [code=html] tags.
<tr><th>Category:</th><td><select name = "cat">
<option value = ""></option>
<option value = "blu" selected>Blu-ray</option>
<option value = "live">Live Action</option>
<option value = "anim">Animation</option>
</select></td></tr>
I just want the default option to be the one that corresponds with a record.
If a record happened to be category Blu-Ray, I would want that option selected in my box, etc.
Last edited by Benjamin on Mon Jun 29, 2009 11:34 pm, edited 1 time in total.
Reason:Changed code type from text to html.
<? // php code looks in database for the selected value
// then lets say user has blu-ray
// set $blu=1;
// don't set the others anywhere else in your script
// only set them if you want that item selected
?>
<tr><th>Category:</th><td><select name = "cat">
<option value = ""></option>
. <option value = "blu" <? if(isset($blu)) echo 'selected';?>>Blu-ray</option>
. <option value = "live" <? if(isset($live)) echo 'selected';?>>Live Action</option>
<option value = "anim" <? if(isset($anim)) echo 'selected';?>>Animation</option>
</select></td></tr>
And now the untested solution is (i assume you have the records somewhere as you should do if you don't have them, because if you want to add/remove an item you will have to edit some html instead of adding/delete a new element in the array):
And we also can create an engine that loads form definition from database and render itself and has also error management based on data. Also implement the decorators pattern to draw the form as i like and we are ready to go .
Your solution is not reusable astions because you are assuming that all the forms will have only that select box and its submit button.