Jump to content

Checkboxes driving me nuts!


vincea

Recommended Posts

I am creating a newsletter management system and what I want it to do is list all the subscribers and their email addresses from the database. Beside each, a checkbox.. when the checkbox is "checked" it will add onto a string that will be the complete recipient list.

 

Right now I have if the box gets checked (with an "onchange") it will put that box value (email address) into a textbox.

 

If Unchecked it will remove the e-mail from the textbox.

 

here is some code:

 

function getChecks() {
	count = document.nl_subs.elements.length;
	for (i=0; i < count; i++)  {
   	 if(document.nl_subs.elements[i].checked == true) {
	 document.getElementById("recipients").value += document.nl_subs.elements[i].value + ",";
 }
}

}

 

I know this is way off.. cuz right now it is getting ALL the checkboxs that are checked and adding them into the textbox.

 

Here is the html & php:

$sub_check is the number (1, 2, 3 etc)

 

<input type="checkbox" name="sub_check<? echo $sub_check ?>" value="<? echo $email ?>" onchange="getChecks()"/>

 

I tried using the getChecks(this.name) do just do the ONE checkbox but i couldnt get it to work.

 

so for the ONE checkbox i had something like:

 

function getChecks(element) {
	checkbox = document.nl_subs.element;
   	 if(document.nl_subs.checkbox.checked == true) {
	 document.getElementById("recipients").value += document.nl_subs.element.value + ",";
 }
}

 

any help is appreciated.

Link to comment
Share on other sites

What if someone unchecks and rechecks the box?

 

<script language="javascript" type="text/javascript">
function getChecks(obj) {
if (obj.checked) {
	document.getElementById("recipients").value += obj.value + ",";
}
}
</script>

<form name="nl_subs">
<input type="checkbox" value="1" name="test_1" onchange="getChecks(this)" />
<input type="checkbox" value="2" name="test_2" onchange="getChecks(this)" />
<input type="checkbox" value="3" name="test_3" onchange="getChecks(this)" />
<textarea id="recipients"></textarea>
</form>

Link to comment
Share on other sites

What if someone unchecks and rechecks the box?

 

<script language="javascript" type="text/javascript">
function getChecks(obj) {
if (obj.checked) {
	document.getElementById("recipients").value += obj.value + ",";
}
}
</script>

<form name="nl_subs">
<input type="checkbox" value="1" name="test_1" onchange="getChecks(this)" />
<input type="checkbox" value="2" name="test_2" onchange="getChecks(this)" />
<input type="checkbox" value="3" name="test_3" onchange="getChecks(this)" />
<textarea id="recipients"></textarea>
</form>

 

thanks that works great and cleans up my ugly code but still I need it when the checkbox is Unchecked now remove it from the text box or string so it isn't part of the mass email

Link to comment
Share on other sites

Why not have a "finalize" type button that loads up the elements before submitting? Is the click on/click off interactivity really needed?

 

Good idea.. i don't know why i always make things so difficult to begin with.. I will try that. 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.