Click to See Complete Forum and Search --> : javascript validataion question/problem


casa
12-01-2003, 03:19 PM
I am dynamically buiding an html form with php. I'm reading a db table field which has a set of codes delimited by a pipe. If the code is 1-2 characters long it's a group and it it's 3-4 characters long it's a deparment within a particular group. My form goes something like this:

-----------------------------------------------------
Date Range: [ Select a Date Range |v]

Group or
Department: [ ] AD select as
[ ] BD many as
[ ] CD01 needed
[ ] CD02
[ ] CD10
-----------------------------------------------------
[ Select ] [ Reset ]
-----------------------------------------------------
In this case, I would have two hidden fields,
<input type="hidden" name="GR" value=2>
<input type="hidden" name="DP" value=3>
so I would know how many possible fields there would be.

The user could select all or any combination of these 5 checkboxes, however, they have to select at least on. Right now, I have them set like this:
<input type="checkbox" name="G1" value="AD">
<input type="checkbox" name="G2" value="BD">
<input type="checkbox" name="D1" value="CD01">
<input type="checkbox" name="D2" value="CD02">
<input type="checkbox" name="D3" value="CD10">

I can't figure out how to validate this correctly. I was thinking about something like:
<input type="checkbox" name="G[1]" value="AD">
<input type="checkbox" name="G[2]" value="BD">
<input type="checkbox" name="D[1]" value="CD01">
<input type="checkbox" name="D[2]" value="CD02">
<input type="checkbox" name="D[3]" value="CD10">

But am not sure exactly how that works. Any pointers or advice would be greatly appreciated.

Thanks,
casa

casa
12-02-2003, 08:21 AM
popping this back up to the top. Need an help on this asap. stuck and can't continue until this is resolved. continuing to research on my own, but don't really know where to start. My javascript books are terrible.

Gollum
12-02-2003, 10:09 AM
Hi casa,

since your checkboxes are all related, you could give them all the same name...

<input type="checkbox" name="GRDP" value="AD">
<input type="checkbox" name="GRDP" value="BD">
<input type="checkbox" name="GRDP" value="CD01">
<input type="checkbox" name="GRDP" value="CD02">
<input type="checkbox" name="GRDP" value="CD10">

then you can check that at least one is checked by...

function validateGRDP()
{
// assuming the form is called "f"
for ( var i = 0; i < document.f.GRDP.length; i++ )
{
if ( document.f.GRDP[i].checked ) return true;
}
return false;
}

Back on the server you can find out what 'values' have been checked using the forms collection - check your server scripting code manual

casa
12-02-2003, 01:21 PM
I need to keep the the group and department seperate.

One question though, since they can check all of the boxes, how do I check for a value in the next php script if they are all named the same?

I need to do something like this:

$sql = "SELECT * FROM table WHERE user = '".$_POST['user']."' ";
for ($i=1;$i<=totaldepts;$i++) {
if ($_POST['D1']) {
$sql .= "AND dept = '".$_POST['D1']." ';
}
}
or something like that. Actually, I will be using the IN clause, but can't remember it off the top of my head. :) So if I don't index it some way, how to I know what I'm getting in the next script?

casa
12-05-2003, 01:35 PM
This is what I was looking for!
var match = 0;
var i;
for (i = 0; i < document.forms[0].elements.length; i++) {
var currentName = document.forms[0].elements[i].name;
if (document.forms[0].elements[i].type == "checkbox") {
if (document.forms[0].elements[currentName].checked == true) {
match++;
}
}
}
if (match == 0) {
alert ("Please check an GROUP or DEPARTMENT.");
return false;
}