Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple checkbox values

Status
Not open for further replies.

MarcLodge

Programmer
Feb 26, 2002
1,886
GB
Hi All,
I've been struggling with this problem for some time and have yet to reach a successful conclusion despite much help. I'm going to post this in HTML, Javascript, and PH forums in hope that SOMEBODY might be able to solve it.

The Objective
To have a number of identically named checkboxes on a page, that indicate an interest in a particular summer camp. The user can check more than one, but must check at least one. The checkbox is validated by a piece of Javascript on a submit button to ensure at least one is checked. The data is then processed by a PHP script which sends an e-mail.

The Problem
I cannot get both the Javascript AND the PHP to work. I can either one, but not both.

The HTML
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps&quot; VALUE=&quot;Camp A&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps&quot; VALUE=&quot;Camp B&quot;>
or
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp A&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp B&quot;>

The Javascript
Campswitch = 0
for (i=0; i<regform.Camps.length;i++)
{ if (regform.Camps.checked)
{ Campswitch = 1
}
}
if (Campswitch == 0)
{ alert(&quot;Please select one or more Camps and dates that you are interested in&quot;)
return false
}

The PHP
$Camps = $HTTP_POST_VARS[&quot;Camps&quot;]);
or
$Camps = implode(&quot;,&quot;, $HTTP_POST_VARS[&quot;Camps&quot;]);

What Happens?
If in the HTML I leave out the square brackets and in the PHP leave out the implode, the Javascript works fine and validates the form correctly, but the PHP script only returns the last value of the checkbox. If I add the square brackets and the implode, the PHP happily passes all values to the email, but the Javascript stops working.

I've messed about with this trying as many different combinations as I can think of, but just can't seem to get it to work how I want. I am so fed up with it, I'm considering allowing no boxes to be checked. Unless anybody has any ideas of what I'm doing wrong.

Grateful thanks in anticipation.
Marc
 
I found that this is nice:

df = document.forms[&quot;your-form&quot;];
counted = 0;
for (ele=0;ele<df.elements.length;ele++) {
part = frm.elements[ele];
if (part.type == &quot;checkbox&quot; && part.name == &quot;Camps[]&quot;) {
counted++;
}
} --BB
 
sorry, that counts the elements that are checkboxes with the name &quot;Camps[]&quot;, if you want to know if its checked, just stick it in the if statement. --BB
 
Thanks for your help BB, but still no joy. It's probably because I'm not too sure of the language, so let me show you what I've got so far:
Script
function validform(regform)
{
Campswitch = 0
for (i=0; i<regform.Camps[].length;i++)
{ if (regform.Camps[].checked)
{ Campswitch = 1
}
}
if (Campswitch == 0)
{ alert(&quot;Please select one or more Camps and dates that you are interested in&quot;)
return false
}
return true
}

The HTML reads
<FORM ONSUBMIT=&quot;return validform(this)&quot; METHOD=&quot;post&quot; action=&quot;formmail.php3&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp A&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp B&quot;>

How exactly do I need to change the JavaScript to make it work, because I've fiddled with it, and it just seems to keep going right on thru, ignoring the fact I've not checked any boxes.
 
This is what you need :

Code:
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
    function validForm (regform) {
        campSwitch = 0;
        for (i=0; i<regform.Camps.length;i++) {
            if (regform.Camps[i].checked) {
                campSwitch = 1;
            }
        }
        if (campSwitch == 0) {
           alert (&quot;Please select one or more Camps and dates that you are interested in&quot;);
            return false;
        }
        return true;
    }
//-->
</script>

<form onsubmit=&quot;return validForm (this);&quot; method=&quot;post&quot; action=&quot;formmail.php3&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps&quot; value=&quot;camp a&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps&quot; value=&quot;camp b&quot;>
<input type=&quot;submit&quot; value=&quot;go&quot;>
</form>
Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Sorry I mean :

Code:
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
    function validForm (regform) {
        campSwitch = 0;
        for (i=0; i<regform.camps.length;i++) {
            if (regform.camps[i].checked) {
                campSwitch = 1;
            }
        }
        if (campSwitch == 0) {
           alert (&quot;Please select one or more Camps and dates that you are interested in&quot;);
            return false;
        }
        return true;
    }
//-->
</script>

<form onsubmit=&quot;return validForm (this);&quot; method=&quot;post&quot; action=&quot;formmail.php3&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps&quot; value=&quot;camp a&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps&quot; value=&quot;camp b&quot;>
<input type=&quot;submit&quot; value=&quot;go&quot;>
</form>
Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Sorry didn't really read your post properly try this :

code]<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function validForm (regform) {
campSwitch = 0;
for (i=0; i<regform[&quot;camps[]&quot;].length;i++) {
if (regform[&quot;camps[]&quot;].checked) {
campSwitch = 1;
}
}
if (campSwitch == 0) {
alert (&quot;Please select one or more Camps and dates that you are interested in&quot;);
return false;
}
return true;
}
//-->
</script>

<form onsubmit=&quot;return validForm (this);&quot; method=&quot;post&quot; action=&quot;formmail.php3&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps[]&quot; value=&quot;camp a&quot;>
<input type=&quot;checkbox&quot; name=&quot;camps[]&quot; value=&quot;camp b&quot;>
<input type=&quot;submit&quot; value=&quot;go&quot;>
</form>[/code] Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top