limitphp Posted April 6, 2009 Share Posted April 6, 2009 I have a bunch of checkboxes in a form. They are all named genre. When I submit the form, and try to catch the value of the checkboxes on the other side with: $genres = $_POST['genre']; It only gets the value of the very last checked checkbox. In other words, if 2 checkboxes are checked, it will only get the value of the last checked one. Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/ Share on other sites More sharing options...
sasa Posted April 6, 2009 Share Posted April 6, 2009 change names of your checkboxes to 'genre[]' Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/#findComment-802495 Share on other sites More sharing options...
limitphp Posted April 6, 2009 Author Share Posted April 6, 2009 change names of your checkboxes to 'genre[]' Is there another way? Thats the way I had it and it does work that way, but it messes up my javascript. function checkRockGenres(){ var num_genres = document.frmGenres.genre.length; var unchecked_flag=0; if (document.frmGenres.allrock.checked){ document.frmGenres.genre[0].checked=true; document.frmGenres.genre[1].checked=true; document.frmGenres.genre[2].checked=true; document.frmGenres.genre[3].checked=true; //CHECK TO SEE IF ALL GENRES ARE CHECKED for (var i=0; i <= num_genres-1; i++) { if (document.frmGenres.genre[i].checked==false) unchecked_flag = 1; } if (unchecked_flag==1) document.frmGenres.allGenres.checked=false; else document.frmGenres.allGenres.checked=true; } else { document.frmGenres.allGenres.checked=false; document.frmGenres.genre[0].checked=false; document.frmGenres.genre[1].checked=false; document.frmGenres.genre[2].checked=false; document.frmGenres.genre[3].checked=false; } } Even when i change the line to: var num_genres = document.frmGenres.genre[].length; it still doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/#findComment-802499 Share on other sites More sharing options...
kenrbnsn Posted April 6, 2009 Share Posted April 6, 2009 Javascript using the "id" attribute, so just append the number onto the string it uses. PHP will use the array in the name. If you post the code of your form, we could help you better. Ken Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/#findComment-802502 Share on other sites More sharing options...
limitphp Posted April 6, 2009 Author Share Posted April 6, 2009 <TR> <TD ALIGN="center" COLSPAN="2" STYLE="padding-top:15px;"> <?php if($allRock==1){ echo "<input type='checkbox' name='allrock' value='1' onclick='javascript:checkRockGenres()' CHECKED />";} else{ echo "<input type='checkbox' name='allrock' value='1' onclick='javascript:checkRockGenres()' />";} ?> <FONT CLASS="genres">All Rock 'n' Roll</FONT> </TD> </TR> <TR> <TD COLSPAN="2"> <FONT CLASS="small-genre"> <?php if(strstr($genres, 'Alternatve')) { ?> <input type="checkbox" name="genre" value="1" onclick='set_major_genres()' CHECKED /> <?php }else{ ?> <input type="checkbox" name="genre" value="1" onclick='set_major_genres()' /> <?php } ?> Alternatve <?php if(strstr($genres, 'Rock')) { ?> <input type="checkbox" name="genre" value="2" onclick='set_major_genres()' CHECKED /> <?php }else{ ?> <input type="checkbox" name="genre" value="2" onclick='set_major_genres()' /> <?php } ?> Rock <?php if(strstr($genres, 'Punk')) { ?> <input type="checkbox" name="genre" value="3" onclick='set_major_genres()' CHECKED /> <?php }else{ ?> <input type="checkbox" name="genre" value="3" onclick='set_major_genres()' /> <?php } ?> Punk <?php if(strstr($genres, 'Indie')) { ?> <input type="checkbox" name="genre" value="4" onclick='set_major_genres()' CHECKED /> <?php }else{ ?> <input type="checkbox" name="genre" value="4" onclick='set_major_genres()' /> <?php } ?> Indie </FONT> </TD> It continues all the way down to name="genre" value="17" Ignore all the if(strstr($genres, 'Alternatve') I need to rework that part. The problem with using id instead of name is this will not work: var num_genres = document.frmGenres.genre.length; Right? That .length can only be used on form objects? Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/#findComment-802507 Share on other sites More sharing options...
limitphp Posted April 6, 2009 Author Share Posted April 6, 2009 solved.... I just changed all references of : document.frmGenres.genre[x].checked=false; to document.frmGenres.elements['genre[]'][x].checked=false; thanks guys... Quote Link to comment https://forums.phpfreaks.com/topic/152812-solved-send-multiple-values-using-checkboxes-with-same-name/#findComment-802532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.