Solved

Dynamically select radio button based on value

Posted on 2011-02-20
4
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

628 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