Solved

Dynamically select radio button based on value

Posted on 2011-02-20
4
369 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now