php - I have custom form loop and the radio value are override -


i have on db: enter image description here

then on backend

<?php      foreach($form['questions'] $question) :          $options = $question['options'];         $options = explode(',',$options); ?>          <div class="form-group">             <input type="hidden" value="<?php echo $question['label']; ?>" name="custom_form[label][]" />             <label><?php echo $question['label']; ?></label>             <?php if($question['type'] == 'text'): ?>                 <input type="text" placeholder="<?php echo $question['label']; ?>" name="custom_form[answer][]" class="form-control" required />             <?php elseif($question['type'] == 'textarea'): ?>                 <textarea name="custom_form[answer][]" placeholder="<?php echo $question['label']; ?>" class="form-control" required></textarea>             <?php elseif($question['type'] == 'radio'): ?>                  <div class="input-group btn-group" data-toggle="buttons">                     <?php foreach($options $option): ?>                         <label class="btn btn-default"><input type="radio" name="custom_form[answer][]" value="<?php echo $option; ?>" required><?php echo $option; ?></label>                     <?php endforeach; ?>                 </div>             <?php elseif($question['type'] == 'select'): ?>                 <select class="form-control" name="custom_form[answer][]" required>                     <option value=""></option>                     <?php foreach($options $option): ?>                         <option><?php echo $option; ?></option>                     <?php endforeach; ?>                 </select>             <?php endif; ?>         </div> <?php     endforeach; ?> 

and result of php, when submit overlap new radio

before submit

enter image description here

after submit

enter image description here

** can see, 2nd radio gone **

edit added html

<div class="col-md-6">     <div class="form-group">         <input type="hidden" name="custom_form[label][]" value="what current immigration status?">         <label>what current immigration status?</label>         <select required="" name="custom_form[answer][]" class="form-control">                 <option value=""></option>                 <option>naturalized citizen</option>                 .....         </select>     </div>      <div class="form-group">         <input type="hidden" name="custom_form[label][]" value="do have visa?">         <label>do have visa?</label>          <div data-toggle="buttons" class="input-group btn-group">             <label class="btn btn-default active"><input type="radio" required="" value="yes" name="custom_form[answer][]">yes</label>             <label class="btn btn-default"><input type="radio" required="" value="no" name="custom_form[answer][]">no</label>         </div>     </div>      <div class="form-group">         <input type="hidden" name="custom_form[label][]" value="which type of visa have?">         <label>which type of visa have?</label>         <select required="" name="custom_form[answer][]" class="form-control">             <option value=""></option>             <option>a-1/a-2</option>             .....         </select>     </div>      <div class="form-group">         <input type="hidden" name="custom_form[label][]" value="what gender?">         <label>what gender?</label>          <div data-toggle="buttons" class="input-group btn-group">             <label class="btn btn-default active"><input type="radio" required="" value="male" name="custom_form[answer][]">male</label>             <label class="btn btn-default"><input type="radio" required="" value="female" name="custom_form[answer][]">female</label>         </div>     </div> </div> 

edit2

when use now, i've got undefined offset error

$custom_forms = $_post['custom_form']; foreach($custom_forms['label'] $key => $question) {     echo htmlentities($question . ' - ' . $custom_forms['answer'][$key]) . '<br>'; } 

<input type="radio" name="custom_form[answer][]" /> 

as far can see, using same name inputs.

since array doesnt bother text inputs.

but radio buttons work differently, have 2 groups of radio buttons same name, although css shows them 2 sepparate entities, when submit form parsed 1 radio button group.

my solution change name of radio inputs like

"custom_form[answer][input_<?php=$question['id']?>]" 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -