Forum Moderators: coopster
I have a form that a partner filles out, included is a few drop down menus, check boxes, and radio buttons.
Everything gets submitted into a database.
I want to have a user then be able to edit that data.
I copy my original form and have it pull the data from the database and echo the values just fine.
I was wondering if there was a way to still use the drop down boxes and have the value from the database be selected instead of the default selection on the drop down or if i have check box checked already if the database says it should be checked.
thanx.
First, get the specific record from the database, a query like "select * from ".$table." where [IDfield] = '".$thisID."'"
Now you will have a $row[] of values that this partner has selected when filling out the form. For a text field, enter the into the value field, like this
value="'.$row['thisfield'].'"
If the field was left blank, and there is no value from the database, then the form field will also be left blank.
For a text box, enter the value in between the <textarea> and </textarea> tags.
For a select list, you will have to check each list item to see if it matches the value from the database. So here, each option's value will the value you also have in the form, and if the value from the database matches the value for this option, then you'd print selected="selected" for that list item.
Radio buttons and checkboxes work the same as the select list; the value is NOT what's been selected by the person filling out the form, but the value you put in the original form. If the value matches the selected value from the database in your $row['thisfield'] variable, then print selected="selected" for that particular item.
HTH
So for each of the option values in the drop downs, do I do a php if statement to see if it matches the value in the database?
Here is one of my drop downs.
<tr>
<td>Country: </td>
<td><select name="country">
<option value="United States">United States</option>
<option value="Canada">Canada</option>
<option value="Puerto Rico">Puerto Rico</option>
</select></td>
</tr>
mg, yes, you would do an if statement to compare. For example
print '<tr>';
print '<td>Country: </td>';
print '<td><select name="country">';
if ($row['country']=='United States')
{ print <option value="United States" selected="selected">United States</option>'; }
else
{ print <option value="United States">United States</option>'; }
if ($row['country']=='Canada')
{ print <option value="Canada" selected="selected">Canada</option>'; }
else
{ print <option value="Canada">Canada</option>'; }
if ($row['country']=='Puerto Rico')
{ print <option value="Puerto Rico" selected="selected">Puerto Rico</option>'; }
else
{ print <option value="Puerto Rico">Puerto Rico</option>'; }
print '</select></td>';
print '</tr> ';
Thank you for not showing me step by step on checkboxes and radios, made me have to think and test till I got it working.
And even the code you did display I had something wrong with it so I had to figure that out.
I don't like just taking code and using it, when I do, i still examine every piece of it so I know what I am doing.
Instead of manually typing each country why don't you put the counties in an array?
<?
$countries[] = "United States";
$countries[] = "Canada";
$countries[] = "Puerto Rico";print '<tr>
<td>Country: </td>
<td><select name="country">';foreach($countries as $county)
{
if($row['country']==$county)
{
print '<option value="'.$county.'" selected="selected">'.$county.'</option>';
}
else
{
print '<option value="'.$county.'">'.$county.'</option>';
}
}print '</select></td>
</tr> ';
?>
Or even better .... create a table for "countries"?
Something for you to think about ....
Del
[ note:- above code hasn't been tested .... ]