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 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 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>
                <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>

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

| improve this question | | | | |
  • So how do you want it to be saved? – Marius May 27 '15 at 7:19
  • It not about save data. i want to get data when i am edit this form – ND17 May 27 '15 at 7:31
  • I don't really understand what you mean – Marius May 27 '15 at 7:34
  • In above using that code make a one form and save data to database. when i am edit that form that time i want to get selected radio and checkbox. understand or not? – ND17 May 27 '15 at 7:48

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

| improve this answer | | | | |
  • your solution is working but its to much broad because i have so many radio button and checkbox . Is it possible to another way which is not too broad – ND17 May 27 '15 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.