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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with checkbox field

Status
Not open for further replies.

novice2004

Programmer
Feb 2, 2004
62
US
Can someone Help me please,
I asked for help at more places without success

I have problem with line
if (eval("document.form2." + boxesName + i + ".checked"))
Even if I check at least one checkbox field
code does not seem to see it.
In my form I need to make sure that at least 1 checkbox is selected.

code never gets behind
if (document.form2[boxesName + i].checked)
{

So I never see
alert( "numberOfBoxesSelected :" + numberOfBoxesSelected );


and I checked all checkboxes.





<script language="JavaScript" type="text/JavaScript">


function checkCheckBoxes(numberOfBoxes, numberToSelect, boxesName, checkBoxObject)
{

var numberOfBoxesSelected = 0;


for (var i=1; i <= numberOfBoxes; i++)
{

//if (eval("document.form2." + boxesName + i + ".checked"))
if (eval("document.form2." + boxesName + i).checked)
{
alert( "numberOfBoxesSelected :" + numberOfBoxesSelected );
numberOfBoxesSelected++;
}
}
}

if (numberOfBoxesSelected < numberToSelect )
{
var elementFocus = document.getElementById(checkBoxObject);
if( elementFocus != null ){ elementFocus.focus(); }
alert(mPrefixCheckBox + "\"" + boxesName + "\"" + mSuffix);
return false;
}
else {return true;}

}




function validateUSPersonalInfo(form)
{
return ( checkCheckBoxes(3,1,"Nursing_Degree", "ND1") )

}


</SCRIPT>



<FORM name="form2" method="post" action=" >
<input type="hidden" name="formname" value="customer_contact.php" >


<TD class=contactUsFont height=5 width="" align="left">

<input type="hidden" name="Nursing_Degree1" value="">
<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree1" value="CNA" id="ND1">CNA

<input type="hidden" name="Nursing_Degree2" value="">

<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree2" value="LPN" id="ND2">LPN

<input type="hidden" name="Nursing_Degree3" value="">
<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree3" value="RN" id="ND3">RN


</TD>
</TR>


<TR>
<TD align="center" colSpan=2>
<a href="javascript:document.form2.submit()" onClick="return validateUSPersonalInfo(form2)">
<img src="../Services/Submit.gif" border=0 alt="Submit" width="73" height="17"></a>
</TD></TR>
 
You need to finish the "if" statement test:
if (eval("document.form2." + boxesName + i).checked == false)

There's always a better way. The fun is trying to find it!
 
Oops... didn't look far enough ahead in your script... put a regular "Submit" button on your form (<input type="submit" value="Submit">) and change your form tag to something like this:

<FORM name="form2" method="post" action=" onSubmit="return validateUSPersonalInfo(this)">


Actually, you could call the checkCheckBoxes() by itself.

There's always a better way. The fun is trying to find it!
 
Does not work.
Thanks for help but it still can't read checkbox even that at least one field is checked.
 
Okay... after looking at your script in detail, you've got more problems then just the validation code! First, you CANNOT have fields with the same name even though you have different ID numbers. Second, you don't need the hidden fields either - you're just creating additional work for yourself and the browser.

I modified your script to check for no check boxes being selected. I'm certain you have other fields but htis will give you a good starting place to finish out the script.

There's always a better way. The fun is trying to find it!
 
Oops, forgot the script.... sorry.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/JavaScript">
function checkCheckBoxes(theForm){
if (theForm.Nursing_Degree1.checked == false || theForm.Nursing_Degree2.checked == false || theForm.Nursing_Degree3.checked == false){
alert("Must select a Nursing Degree");
      theForm.Nursing_Degree1.focus();
			return false;}
}
</SCRIPT>  
</head>
<body>

<FORM name="form2" method="post" action="[URL unfurl="true"]http://www.healthcare.com/form/submit.php?go=1"[/URL] onSubmit="return checkCheckBoxes(this)">
<input type="hidden" name="formname" value="customer_contact.php" >

<table>
<tr><TD class=contactUsFont height=5 width="" align="left">
<INPUT type=checkbox name="Nursing_Degree1" value="CNA" id="ND1">CNA
<INPUT type=checkbox name="Nursing_Degree2" value="LPN" id="ND2">LPN
<INPUT type=checkbox name="Nursing_Degree3" value="RN" id="ND3">RN
</TD></TR>
<TR><TD align="center" colSpan="2">
<input type="submit" value="Submit">
</TD></TR>
</table>
</body>
</html>

There's always a better way. The fun is trying to find it!
 
Thanks but still did not help.
I need hidden fields.


The reason behind my hidden fields is that after php
script does its work and insert data to data file, they can
be easily manipulated with excel and data are properly aligned in columns.
Without hidden fields it depends on how many selections(checkboxes selected) were made and the result vary.

example:

nurse | cna | doctor
nurse | doctor
which is not good
 
That is what I need

nurse | cna | doctor
nurse | | doctor
 
Having hidden fields is okay but you've got the hidden fields having the same name as this input fields. When the form is passed on to your PHP script, only the names and values are passed along, not the ID numbers. Consequently, on 3 of the fields will get passed along and may or may not be the ones you want. (Your PHP script can be written to use just the regular input fields. Hidden fields ARE NOT necessary when passing form data to server-side scripts. All the "HIDDEN" does for you is to tell the browser not to display them. Other then that, they are still part of the form.

Anyway, you should be able to see how to test if check boxes are checked and you should be able to figure out how to count the number checked.

There's always a better way. The fun is trying to find it!
 
Those hidden fields have to have the same name otherwise
they would not have any meaning in my code and my data file would end up messed up.
I guess I will have to give up the function verifying checkboxes.
Thanks
 
You don't have to give up validating the check boxes. Let's go back to the beginning... What MUST to be checked in order to get valid information from the user? We'll start from there and add on as we go. It would be helpful if you could provide a link to the page or post the COMPLETE html code.

There's always a better way. The fun is trying to find it!
 
What MUST to be checked in order to get valid information from the user?
at least one of the following:
(it works when I don't use hidden fields but that is the thing: I need those hidden fields)


<TD class=contactUsFont height=5 width="" align="left">

<input type="hidden" name="Nursing_Degree1" value="">
<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree1" value="CNA" id="ND1">CNA

<input type="hidden" name="Nursing_Degree2" value="">

<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree2" value="LPN" id="ND2">LPN

<input type="hidden" name="Nursing_Degree3" value="">
<INPUT type=checkbox onFocus="promptEntry('')" name="Nursing_Degree3" value="RN" id="ND3">RN


</TD>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top