Jump to content

restoring radio button groups


stephenl

Recommended Posts

Hi,

 

I have a group of radio buttons, formated as a form inorder to store a numeric value in a MySQL database....

 

The form works OK when storing the didgit...

 

My problem is that I also need to read the database and display the numeric value as a radio button selection, ie the reverse of storing the data, inorder to accept modifications to the original data

 

Any suggestions would be appreciated

 

Sample Radio Button Group Form :-

 

<form id="form1" name="form1" method="post" action="">

<table width="200">

<tr>

<td><label>

<input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" />

Button1</label></td>

</tr>

<tr>

<td><label>

<input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" />

Button2</label></td>

</tr>

<tr>

<td><label>

<input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2" />

Button3</label></td>

</tr>

</table>

</form>

 

Thank you

Stephen

Link to comment
https://forums.phpfreaks.com/topic/118062-restoring-radio-button-groups/
Share on other sites

Okay, pretty easy. Should be in PHP Help, unless I misunderstood you.

 

So you want the selected radio button to be checked when you re-visit the page?

 

First you need to grab the value.

//Change this query to the appropriate table and change the WHERE clause to suit your needs.
$query = mysql_query("SELECT * FROM `table` WHERE `whatever`='somevar'") or die("Error: ".mysql_error());
$data = mysql_fetch_array($query);

$selected_radio = $data['radio_field'];

 

You can that with mysql_result() if you want, whatever works.

 

Now. Now we can make an array of all the radios and store selected='selected' in the right one.

 

$radios = array("","","");

switch($selected_radio){
case 1:
$radios[0] = "selected='selected'";
break;
case 2:
$radios[1] = "selected='selected'";
break;
case 3:
$radios[2] = "selected='selected'";
break;
}

 

Now that we have our three radio values in, only one will contain selected='selected' and that one will be the previously selected value.

So your form now becomes this:

 

<form id="form1" name="form1" method="post" action="">
<table width="200">
<tr>
<td><label>
<input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" <?php echo $radios[0]; ?>/>
Button1</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1"  <?php echo $radios[1]; ?>/>
Button2</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2"  <?php echo $radios[2]; ?>/>
Button3</label></td>
</tr>
</table>
</form>

 

Hope that makes sense and you can put some of this to use.

 

Good luck.

Hi

 

Thank you for your reply (sorry about the wrong forum  :-[ )....

 

I used your suggestion, however the "selected='selected'"; does not seem to work with IE7 ??...

 

I replaced this with "checked='checked'"; and everything is fine....

 

Is this a good choice ??...will it be backward compatiable ??

 

Thank you

Stephen

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.