php - Dynamic form radio buttons mess up foreach loop mysql insert -
i have dynamic form has dynamic arrays being set php mysql insert script. records inserted correctly if have of radio buttons checked. if leave unchecked random records inserted. it's attendance table , inputs below repeat each person. i'm checking option radio button.
i've looked @ js hack of assigning hidden input , disabling if 1 of radio buttons checked find little weird.
my radio buttons have value of 1 attend or 2 dive , i'm checking value in script. have +1 set on [$key+1] works if radios checked. appreciate pointing me in correct direction setting loop grab data in array when radio checked.
thanks looking
the 2 offending radio buttons setup rest off variables being set hidden fields , filled actual values. gets insert if don't use radio buttons in form or if they're checked.
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$dive_points.'"> <input type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$attend_points.'"> <input type="hidden" name="_rowid_[]" value="' .$row1['_rowid_']. '"> <input type="hidden" name="location_date[]" value="' .$location_date. '"> <input type="hidden" name="location_name[]" value="' .$location_name. '"> <input type="hidden" name="first_name[]" value="' .$row1['first_name']. '"> <input type="hidden" name="last_name[]" value="' .$row1['last_name']. '"> <input type="hidden" name="cert_agency[]" value="' .$row1['cert_agency']. '"> <input type="hidden" name="high_cert[]" value="' .$row1['high_cert']. '"> <input type="hidden" name="email[]" value="' .$row1['email']. '"> <input type="hidden" name="phone_number[]" value="' .$row1['phone_number']. '"> <input type="hidden" name="photo_release[]" value="' .$row1['photo_release']. '"> <input type="hidden" name="diver_pic[]" value="' .$row1['diver_pic']. '"> <input type="hidden" name="submitted_email[]" value="' . $submitted_email . '"> <input type="hidden" name="food_option[]" value="' .$row2['food_option']. '">
my php loop setup this.
foreach ($_post['location_date'] $key => $value) { $_rowid_ = $_post['_rowid_'][$key]; $location_date = $_post['location_date'][$key]; $location_name = $_post['location_name'][$key]; $first_name = $_post['first_name'][$key]; $last_name = $_post['last_name'][$key]; $cert_agency = $_post['cert_agency'][$key]; $high_cert = $_post['high_cert'][$key]; $email = $_post['email'][$key]; $phone_number= $_post['phone_number'][$key]; $photo_release = $_post['photo_release'][$key]; $diver_pic = $_post['diver_pic'][$key]; $submitted_email = $_post['submitted_email'][$key]; $food_option = $_post['food_option'][$key]; foreach ($_post['dive_attend_points'][$key+1] $row) { $dive_attend = $row['dive_attend']; if (!empty($dive_attend)) { if($dive_attend == 1) { $dive_points = 0; $attend_points = 1; } elseif($dive_attend == 2) { $dive_points = 2; $attend_points = 0; } } } if($dive_attend == 1 || $dive_attend == 2){ //mysql insert here. } }
i'm not sure why i've never seen or heard of until saw in post while doing research. keep arrays aligned same indexes assign index dynamically or manually. in case used same '.$row1['_rowid_'].'
using give radio button group unique name grouped properly. placed inside empty [] each input. technique allowed me not use clunky js hacks.
html
<input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$dive_points.'"> <input type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$attend_points.'"> <input type="hidden" name="_rowid_['.$row1['_rowid_'].']" value="' .$row1['_rowid_']. '"> <input type="hidden" name="location_date['.$row1['_rowid_'].']" value="' .$location_date. '"> <input type="hidden" name="location_name['.$row1['_rowid_'].']" value="' .$location_name. '"> <input type="hidden" name="first_name['.$row1['_rowid_'].']" value="' .$row1['first_name']. '"> <input type="hidden" name="last_name['.$row1['_rowid_'].']" value="' .$row1['last_name']. '"> <input type="hidden" name="cert_agency['.$row1['_rowid_'].']" value="' .$row1['cert_agency']. '"> <input type="hidden" name="high_cert['.$row1['_rowid_'].']" value="' .$row1['high_cert']. '"> <input type="hidden" name="email['.$row1['_rowid_'].']" value="' .$row1['email']. '"> <input type="hidden" name="phone_number['.$row1['_rowid_'].']" value="' .$row1['phone_number']. '"> <input type="hidden" name="photo_release['.$row1['_rowid_'].']" value="' .$row1['photo_release']. '"> <input type="hidden" name="diver_pic['.$row1['_rowid_'].']" value="' .$row1['diver_pic']. '"> <input type="hidden" name="submitted_email['.$row1['_rowid_'].']" value="' . $submitted_email . '"> <input type="hidden" name="food_option['.$row1['_rowid_'].']" value="' .$row2['food_option']. '">
the adjusted php
foreach ($_post['location_date'] $key => $value) { $_rowid_ = $_post['_rowid_'][$key]; $location_date = $_post['location_date'][$key]; $location_name = $_post['location_name'][$key]; $first_name = $_post['first_name'][$key]; $last_name = $_post['last_name'][$key]; $cert_agency = $_post['cert_agency'][$key]; $high_cert = $_post['high_cert'][$key]; $email = $_post['email'][$key]; $phone_number= $_post['phone_number'][$key]; $photo_release = $_post['photo_release'][$key]; $diver_pic = $_post['diver_pic'][$key]; $submitted_email = $_post['submitted_email'][$key]; $food_option = $_post['food_option'][$key]; $dive_attend = $_post['dive_attend_points'][$key] if($dive_attend == 1) { $dive_points = 0; $attend_points = 1; } elseif($dive_attend == 2) { $dive_points = 2; $attend_points = 0; } //mysql insert here. } //end of each
Comments
Post a Comment