Solved

Dynamically select radio button based on value

Posted on 2011-02-20
4
372 Views
Last Modified: 2012-05-11
Hi there,

I have some code that I changed from a select box to a list of radio buttons. What I don't know how to do is make the first radio button in the list checked as default.
 <?php while (wpsc_have_variation_groups()) : wpsc_the_variation_group(); ?>

<label for="<?php echo wpsc_vargrp_form_id(); ?>"><?php echo wpsc_the_vargrp_name(); ?>:</label>
<br />

<?php /** the variation HTML and loop */?>

<?php while (wpsc_have_variations()) : wpsc_the_variation(); ?>

<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" name="variation[<?php echo wpsc_vargrp_id(); ?>]" class="wpsc_select_variation" id="<?php echo wpsc_vargrp_form_id(); ?>"  />
<?php echo wpsc_the_variation_name(); ?>
<br />
<?php endwhile; ?>

<?php endwhile; ?>

Open in new window


There will only be 3 radio buttons and they have a value of 1 2 and 3. I need value 1 to be checked each time that page originally loads, and then they can change it to other options if they need to.

Thanks for your help




0
Comment
Question by:carlyblack
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:MatthewP
ID: 34939790
As far as the HTML goes all you need to do is add the attribute "checked" to it

<input type="radio" name="name" value="value" checked="checked" />

A quick and easy way to do this for the first one only in your php would be modifying your lines 8-13 above with this:

$checked="checked"
<?php while (wpsc_have_variations()) : wpsc_the_variation(); ?>

<input type="radio" value="<?php echo wpsc_the_variation_id(); ?>" name="variation[<?php echo wpsc_vargrp_id(); ?>]" class="wpsc_select_variation" id="<?php echo wpsc_vargrp_form_id(); ?>"  checked="<?php echo $checked; ?>" />
<?php echo wpsc_the_variation_name(); ?>
<br />
<?php $checked=""; ?>
<?php endwhile; ?>

Open in new window



0
 

Author Comment

by:carlyblack
ID: 34939818
Hi Thanks for your quick response.

when I did this it showed the last one in the list as checked, and then when I viewed the source code each of the options had checked="checked"

Any further ideas please?
0
 

Author Comment

by:carlyblack
ID: 34939826
sorry, just made a slight adjustment and now it's only showing in the source code as the first one checked="checked" but it's actually checking the 3rd on in the list.
0
 
LVL 6

Accepted Solution

by:
MatthewP earned 500 total points
ID: 34940057
Try loading the page from scratch rather than refreshing, if you've checked the 3rd one anyway the browser may be remembering your setting.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question