Forum Moderators: coopster

Message Too Old, No Replies

making radio buttons sticky

during validation and dB retrieval

         

knotworking

6:23 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



My new form challenge I'm facing is preserving my checked radio buttons after form validation and, checking the appropriate button when information is retrieved from mysql dB.

I (with lots of help) was able to get all of the above working for an array of checkboxes but, I cannot use the same method for the radio buttons (I was able to call and display them dynamically but, that didn't seem to help. Here's the code I'm strugling with:

<label>( National Touring
<input type="radio" name="touring" value="National Touring"></label>)
&nbsp;
<label>( Regional Touring
<input type="radio" name="touring" value="Regional Touring"></label>)
&nbsp;
<label>( Market-based
<input type="radio" name="touring" value="Market-Based"></label>)
&nbsp;
<label>( Other
<input type="radio" name="touring" value="Other"></label>)

Can anybody teach me how this can be done? Much Thanks in advance.

dreamcatcher

9:00 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

To display which box is checked with data from a database, use a ternary operator.

Something like:

<input type="radio" name="touring" value="National Touring"<?php echo (($row['field']=="National Touring")? " checked" : "");?>>

Or is that not what you wanted?

dc

knotworking

9:32 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



I'll give that a whirl, thanks! I'm curious if that will retain the selected value (initial register form, not called from the dB) when the form is validated.

Here's the last code I was trying, it seems similiar.

$touring = $_POST['touring'] ;
echo '<input type="radio" name="$touring" value="' .$value. '"';
echo ( in_array($value, $touring) )? ' CHECKED ' : '';
echo "> $value</input>";

I was able to get it to display the array of values, but not retain the selected value after validation.

knotworking

2:34 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



Got it working using this:

<?
print '
<b>National Touring</b>
<input type="radio" name="touring" value="National Touring"';
if ($_POST['touring'] == 'National Touring') echo 'checked="checked" ';
print '/>';
?>