php - Unable to retrieve values from checkbox array -


i have multiple checkboxes(array) codes below:

                            <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="business" /> business</label>                             </div>                             <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="public affairs / law" />public affairs / law</label>                             </div>                             <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="medicine" />medicine</label>                             </div>                             <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="literature / writing / journalism" />literature / writing / journalism</label>                             </div>                             <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="technology" />technology</label>                             </div>                             <div class="checkbox">                                 <label><input type="checkbox" name="major[]" value="engineering" />engineering</label>                             </div> 

when form submitted, tried retrieve selected values by:

         print_r($_post['major']); 

it prints:

         array ( [0] => [1] => ) 

it detecting 2 checkboxes ticked somehow unable retrieve values out. doing wrong?

=======================php snippets============================

require 'dbconnect.php';   if($_server["request_method"] == "post"){                  if(isset($_post['major'])){                  $top_majors = implode(',', $_post['top_three_major']);                  echo $top_majors;             }else{                  $top_majors = '-';              } } 

=======================solved============================

ok, solved problem. stupid mistake try clear form fields when browser ready, thus, checkbox values reset before able post.

thank guys helping me! learner student, appreciate that! hope can vote bad reputation far low.

thanks again.

i tried code. there quotes missing. after addign thos worked well.

 <div class="checkbox">                             <label><input type="checkbox" name="major[]" value="business" /> business</label>                         </div>                     <div class="checkbox">                             <label><input type="checkbox" name="major[]" value="public affairs / law" />public affairs / law</label>                         </div>                         <div class="checkbox">                             <label><input type="checkbox" name="major[]" value="medicine" />medicine</label>                         </div>                         <div class="checkbox">                             <label><input type="checkbox" name="major[]" value="literature / writing / journalism" />literature / writing / journalism</label>                         </div>                         <div class="checkbox">                             <label><input type="checkbox" name=major[] value="technology" />technology</label>                         </div>                         <div class="checkbox">                             <label><input type="checkbox" name="major[]" value="engineering" />engineering</label>                         </div> 

--php---

print_r($_post['major']); 

--out put-----

array ( [0] => medicine [1] => literature / writing / journalism [2] => technology ) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -