Jump to content

Loop through checkboxes and check if selected.


deezy

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.