Changing Select Box into Radio

Status
Not open for further replies.

Exel

Active Member
2,074
2008
543
100
I'm trying to change a code that selects options for a product to radio selections instead.

Original Code:

Code:
<select name="option[<?php echo $option['option_id']; ?>]">
    <?php foreach ($option['option_value'] as $option_value) { ?>
    <option value="<?php echo $option_value['option_value_id']; ?>"><?php echo $option_value['name']; ?>
    <?php if ($option_value['price']) { ?>
    <?php echo $option_value['prefix']; ?><?php echo $option_value['price']; ?>
    <?php } ?>
    </option>  
    <?php } ?>
</select>

My Code:
Code:
<tr>
<?php echo $option['name']; ?>:<br />
	<?php foreach ($option['option_value'] as $option_value) { ?>
<td>
	<label for="option-value-<?php echo $option_value['option_value_id']; ?>"><?php echo $option_value['name']; ?></label>
<br />
    <input type="radio" name="option[<?php echo $option['option_id']; ?>]" value="<?php echo $option_value['option_value_id']; ?>" id="option-value-<?php echo $option_value['option_value_id']; ?>" />
</td>
 <?php } ?>
</tr>

It semi-works, but for some reason, it only grabs the value of one of the 'options.'

For example, the product is an Apple iPod.

There's an option to add the 1GB version or the 8GB version, it only adds the 8GB version.

I checked all the variables, and they're working properly.

0kIdnst.png


Help? :|
 
5 comments
So basically you want to update the option value. You could try javascript to change it with jQuery like

Code:
$('input:radio').change(function() {
   var normalValue = $('#value').text();
   var normalLabel = normalValue.substring(0, 1);
   normalValue = normalValue.substring(1);
   normalValue = normalValue.replace(",","");
   var newValue = $('input:radio:checked').next().text();
});

or

<option selected="selected">

Just a thought but i can't know for sure unless i see the full code. I'll be on skype if you still need help ;)
 
Because you're using loops, it's pretty difficult to do what you need to do; but you just need to add 'checked' to your default option.

Code:
    <input type="radio" name="option[<?php echo $option['option_id']; ?>]" value="<?php echo $option_value['option_value_id']; ?>" id="option-value-<?php echo $option_value['option_value_id']; ?>" [B]checked="checked"[/B] />

But, again, since you're using loops you can't simply add it to your code, because every single one of your options would come out checked. I recommend pushing this into a function or a class, so you can run checks to see if it's the first returned result, then adding
Code:
checked="checked"
if it is.

EDIT: You could use jQuery, but I really don't recommend it, keep in mind, a ton of idiots still use outdated IE 6-8 which is notoriously bad with jQuery DOM manipulation -- but at the very least, it's a quick fix until you figure something else out.
 
Last edited:
It semi-works, but for some reason, it only grabs the value of one of the 'options.'
Its a radio input, so it will only work with the selected option. If you want multiple choice, then go for check-boxes

Could you be more clear on what you want to achieve?
 
Status
Not open for further replies.
Back
Top