...

View Full Version : setting and passing checkbox values??



charbort
01-15-2003, 11:21 PM
I have a name text input and 4 checkboxes and I want to be able to check to see which ones are checked and then have their values and the name value to be transfered to a confirmation type page.

Here's what I have so far, but still reading on how to write this javaScript stuff. Thanks in advance.


--------------------

<html>
<head>
<script language="JavaScript">

function setValue()
{
var selected
for (i=0; i<document.signUp.chkBox.length;i++)
{
if (document.signUp.chkBox[i].checked==true)
selected=i
}
}

</script>
</head>
<body>

<form name="signUp" action="confirm.html" method="post" onSubmit="setValue()">

<input type="hidden" name="selected" value="(this.selected)">
<input type="text" size="30" name="userName" value="">
<input type="checkbox" name="chkBox" value="1">
<input type="checkbox" name="chkBox" value="2">
<input type="checkbox" name="chkBox" value="3">
<input type="checkbox" name="chkBox" value="4">
</form>

</body>
</html>

ez4ne12c
01-15-2003, 11:26 PM
I would start by using different names for the checkboxs , i only use the same name for things like radio buttons cause one affects the other, this is not so with checkboxes..once you have done that you should be able to work out whether each individual check box has been checked and i think your problem will disappear..
ez

charbort
01-15-2003, 11:40 PM
if I change the names of the checkboxes from "chkBox" to say "1", "2", "3", etc, how do I loop through to check them all?

thanks

ez4ne12c
01-16-2003, 12:49 AM
This code works but you may want to pretty it up

<form name='boxes'>
<input name='a1' type='checkbox' checked='true'>
<input name='a2' type='checkbox'>
<input name='a3' type='checkbox'>
<input name='a4' type='checkbox' checked='true'>
<input name='a5' type='checkbox'>
<input name='a6' type='checkbox'>
<input name='a7' type='checkbox'>
<input name='a8' type='checkbox' checked='true'>
<input name='a9' type='checkbox'>
<input name='a10' type='checkbox'>
</form>
<script>
for (i=1; i<=10; i++)
{if (eval("document.boxes.a"+i+".checked")==true) {alert('yep checkbox #'+i);} else {alert ("nope checkbox #"+i)};}

</script>

ez

ez4me2c3d
01-16-2003, 12:57 AM
this line could read

if (eval("document.boxes.a"+i+".checked")) {alert('yep checkbox #'+i);} else {alert ("nope checkbox #"+i);}

PS nice name

ez4ne12c
01-16-2003, 01:07 AM
Thanks
nice name yourself!
ez

charbort
01-16-2003, 01:12 AM
sorry, I'm a little slow today after looking at code and books all day.

I'm trying your suggestion right now.

Thanks,
Chris

charbort
01-16-2003, 01:17 AM
:confused: Wow, I am slow today. Just realized that you guys were talking to each other, "the ez's".

Maybe I should change my name to "nothingIzEz4me" so I can confuse myself further.

ez4ne12c
01-16-2003, 01:28 AM
Thats not completely true
im ez4ne1
i just added the 2c as a disguise hehe
ez

charbort
01-16-2003, 01:58 AM
Alright! I have that part working thanks to your help.
Now if I want to change the value of the checkboxes and send that information to another window, how would I start?

Do I want to set up the variables before or after I pass them to another page?

Thanks,
Chris

------Here is my code:-----

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript">

function whichChecked(){
for (i=1; i<=10; i++)
{if (eval("document.signUp.a"+i+".checked")==true) {alert('yep checkbox #'+i);} else {alert ("nope checkbox #"+i)};}
}
</script>

</head>

<body>
<table width="395" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><form name="signUp" method="post" action="URL_of_confirm_page.html" onSubmit="whichChecked();">
<table width="395" border="0" cellspacing="3" cellpadding="0">
<tr>
<td><table width="395" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="61">Name</td>
<td width="328"><input type="text" name="name"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="395" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26"><input name='a1' type='checkbox'></td>
<td width="363">Hunting</td>
</tr>
<tr>
<td><input name='a2' type='checkbox'></td>
<td>Fishing</td>
</tr>
<tr>
<td><input name='a3' type='checkbox'></td>
<td>OHV</td>
</tr>
<tr>
<td><input name='a4' type='checkbox'></td>
<td>ALL</td>
</tr>
</table></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum