JGeer
JGeer

Reputation: 1852

Change input select to radio

I want to change my select code to radio buttons:

My current code is:

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
        <select name="recommend"  id="recommend_field" class="input-text" style="width: 360px;">
            <option></option>
            <?php foreach ( $this->getOptions() as $option ): ?>
            <option value="<?php echo $option['value'] ?>"><?php echo $this->__($option['label']) ?></option>
            <?php endforeach ?>
        </select>
    </div>
</li>

I tried to change it into this:

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
            <?php foreach ( $this->getOptions() as $option ): ?>
            <label>
            <input type="radio" class="radio" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo __($option['label']) ?></input>
            </label>
            <?php endforeach ?>
    </div>
</li>

What am I doing wrong?

Currently I can select both values and it should only be possible to select 1 radio button.

Upvotes: 0

Views: 108

Answers (4)

Amar Kamthe
Amar Kamthe

Reputation: 2582

Just give same name to all your radio buttons

Your code would look like

<li>
    <label for="recommend_field">
        <?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
        <?php foreach ( $this->getOptions() as $option ): ?>
        <label>
            <input type="radio" class="radio" name="rdoSelect" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value']==$ value) echo ' checked="checked"' ?>>
            <?php echo __($option[ 'label']) ?>
            </input>
        </label>
        <?php endforeach ?>
    </div>
</li>

Upvotes: 3

Satpal
Satpal

Reputation: 133403

You need to add the name of your radio group in the name attribute of radio button.

<input name="groupname" type="radio" class="radio" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value'] == $ value) echo ' checked="checked"' ?>>

Upvotes: 0

Prasanna
Prasanna

Reputation: 779

I have added name attribute for radio button, you are not added.

<li>
    <label for="recommend_field">
        <?php echo $this->__('Would you recommend this product to a friend?') ?>
    </label>
    <div class="input-box">
        <?php foreach ( $this->getOptions() as $option ): ?>
        <label>
            <input type="radio" class="radio" name="radio-group" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value']==$ value) echo ' checked="checked"' ?>>
            <?php echo __($option[ 'label']) ?>
            </input>
        </label>
        <?php endforeach ?>
    </div>
</li>

Upvotes: 0

Ashish Bhagat
Ashish Bhagat

Reputation: 420

you forgot to add name attribute

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
            <?php foreach ( $this->getOptions() as $option ): ?>
            <label>
            <input name="recommend" type="radio" class="radio" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo __($option['label']) ?></input>
            </label>
            <?php endforeach ?>
    </div>
</li>

Upvotes: 0

Related Questions