Magento 1.9 – How to Get Data of Radio Button and Checkbox in Frontend Form

frontendmagento-1.9

I have one form in frontend. In that i have so many radio button and checkbox.

Now when i edit that form that time i want set data of radio button and checkbox.

my phtml

<div id="edit_fri">
        <li><?php echo $this->__("FRI");?> </li>
        <li><input class="allday" type="checkbox" name="availableday[Friday][9-12]" value="9AM - 12AM"></li>
        <li><input class="allday" type="checkbox" name="availableday[Friday][12-6]" value="12PM - 6PM"></li>
        <li><input class="allday" type="checkbox" name="availableday[Friday][6-9]" value="6PM - 9PM"></li>
</div>
<div id="edit_sat">
        <li><?php echo $this->__("SAT");?> </li>
        <li><input class="allday" type="checkbox" name="availableday[Saturday][9-12]" value="9AM - 12AM"></li>
        <li><input class="allday" type="checkbox" name="availableday[Saturday][12-6]" value="12PM - 6PM"></li>
        <li><input class="allday" type="checkbox" name="availableday[Saturday][6-9]" value="6PM - 9PM"></li>
</div>
<div class="field">
    <label class="required"><em>*</em><?php echo $this->__('Gender');?></label>
        <div class="input-box">
            <input type="radio" id="male" class="validate-one-required" name="gender" value="0" >
                <label for="male">
                    <span class="m-selected-radio-button" title="male"><?php echo $this->__("Male");?></span>
                </label>
                <input type="radio" id="female" name="gender" value="1" >
                    <label for="female">
                        <span class="m-selected-radio-button" title="female"><?php echo $this->__("Female");?></span>
                    </label>
        </div>
    </div>

In radion button save value like 0 or 1,but in checkbox i save string like this

{"Friday":{"12-6":"12PM - 6PM","6-9":"6PM - 9PM"},"Saturday":{"12-6":"12PM - 6PM","6-9":"6PM - 9PM"}}

so how to get selected radio button and checkbox

Best Answer

I assume when you edit the form you have an array that contains the data you want to edit. Let's call that array $data.
You can check if the checkbox with the name availableday[Friday][9-12] should be checked like this:

if (isset($data['Friday']['9-12']) && $data['Friday']['9-12'] == '9AM - 12AM') {
    $checked = ' "checked"="checked"';
} else {
    $checked = "";
}

Then make the input look like this:

<li><input class="allday" type="checkbox" name="availableday[Friday][9-12]" value="9AM - 12AM"<?php echo $checked;?>></li>

You should do the same for all the other checkboxes.

for radio do the same but check if ($data['gender'] == 1) or 0

Related Topic