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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.