Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically select radio button based on value

Posted on 2011-02-20
4
Medium Priority
?
378 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

704 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