Forum Moderators: coopster

Message Too Old, No Replies

is it possible to echo a selection for a drop down or check box?

         

mgworek

3:08 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



I have users that I want to go to a form, that pulls data from database and populates the fields based on that data.

I have some check boxes and drop downs on the original form that I would like to carry over to this form so they can still change those options.

is it possible?

bomburmusicmallet

4:13 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



Hey mg, could you please explain again what you're trying to do? I do a lot of work with forms and databases, including building forms with data from the database as well as putting data from responses to forms.

mgworek

4:17 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



sure, sorry.

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.

bomburmusicmallet

4:37 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



Oh yes, this is quite possible! Here's the logic and some pseudo-code.

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

whoisgregg

4:48 pm on Apr 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Radio buttons and check boxes use checked="checked" instead of selected="selected"

mgworek

6:03 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



sorry if I sound like a moron but I am only 2 months into learning php so I am still learning.

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>

bomburmusicmallet

6:10 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



Thank you Greg! I missed that one.

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

mgworek

7:01 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



thank you!

I was thinking it was going to be kinda like that after you first said it could be done.

I got the pull down working, the one with 60 options will not be fun but oh well, hehe.

Now I take the check boxes.

Thanx again!

mgworek

9:07 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



I have it all working.

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.

bomburmusicmallet

11:22 pm on Apr 21, 2006 (gmt 0)

10+ Year Member



mg, that's awesome! You are right, figuring it out is the best way to learn, but having a place to start is even better.

whoisgregg

1:48 pm on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also write a function that accepts an array of value => label and an array of selected values. Then it's just a matter of looping through and doing the comparison rather than hand coding the comparisons.

scriptmasterdel

2:21 pm on Apr 24, 2006 (gmt 0)

10+ Year Member



Whilst on the subject ;)

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