Solved

Dynamically select radio button based on value

Posted on 2011-02-20
4
368 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

16 Experts available now in Live!

Get 1:1 Help Now