deezy Posted July 15, 2008 Share Posted July 15, 2008 Hi, I have a php page containing 5 checkboxes all named ch but with different values. The checkboxes values are all table names within my database. I need to be able to loop through each checkbox and if its been selected i need that table to be cleared out. I know how to clear the table its just the looping through the ch checkboxes and the if statement to see if its been selected im unsure about. Thanks very much. Quote Link to comment Share on other sites More sharing options...
taith Posted July 15, 2008 Share Posted July 15, 2008 you'd want to name your checkboxes like "ch[]"... then on the submitted page... you... foreach($_POST[ch] as $k=>$v){ if($v=='on') #do your clearing here } Quote Link to comment Share on other sites More sharing options...
JD* Posted July 15, 2008 Share Posted July 15, 2008 I'm not sure if you need to even check the "on" state...I think with check boxes they only get added to the array if they are checked. If they are not checked, they are not even posted, key or value. Quote Link to comment Share on other sites More sharing options...
taith Posted July 15, 2008 Share Posted July 15, 2008 I'm not sure if you need to even check the "on" state...I think with check boxes they only get added to the array if they are checked. If they are not checked, they are not even posted, key or value. true... however in keeping with good programming practices, and who knows what'll happen when html5 or php6 comes out... going back over code to fix these issues is a pain... Quote Link to comment Share on other sites More sharing options...
anon_login_001 Posted July 15, 2008 Share Posted July 15, 2008 JD* is correct. The values that get posted are not related to the HTML version rendered, rather the browser sending certain data. Since probably 60+ percent of the world's online forms would be broken if they changed a standard like that, I highly doubt that simply checking for available checkboxen is at all something to concern yourself with. And in another case, when a checkbox is sent, it's name and it's value are sent... no such 'on' state... therefore if they are all named the name and, as deezy says, if all of their values are different, you would still simply check for existing values, since there is no such "On" or "Off" state of a checkbox. Quote Link to comment Share on other sites More sharing options...
deezy Posted July 16, 2008 Author Share Posted July 16, 2008 Thanks for all your replies, this seems to work great. One more thing. I have a Js function using the ch checkboxes which has stopped working, obviously as ive changed the name to ch[]. Ive tried re-naming the values in this function but cant seem to get it working, what am i missing? function confirmDelete() { var total="" for(var i=0; i < document.f1.ch.length; i++){ if(document.f1.ch[i].checked) total +=document.f1.ch[i].value + "\n" } if(total=="") { alert("No tables selected to clear"); return false ; } if(total!="") { var agree=confirm("Are you sure you want to clear the following tables: \n\n" + total); if (agree) return true ; else return false ; } } thanks again! Quote Link to comment 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.