...

View Full Version : Need Help with Getting a Checkbox Array



xfinnerjx
06-11-2010, 01:36 PM
Hi thier,

I am using PHP to generate a page with a list of checkbox's on it. Then I am using JavaScript to validate that the user has selected at lest 1 of the given box's before I do a post back.

But I cannot get my Javascript to see my CheckBox Array.

Any help you can offer would be very useful thank you all in advance.

Here is my PHP code used to build to Checkbox list.


while ($row = mysql_fetch_array($data, MYSQL_ASSOC))
{
if(is_null($row['Team_Number']))
{
$str1 = sprintf('<input type="checkbox" name=\'People\' value="%s">',$row['Member_ID']);
}
else if($row['Team_Number'] == $_REQUEST['T'])
{
str1 = sprintf('<input type="checkbox" name=\'People\' value="%s" checked>',$row['Member_ID']);
}
else
{
$str1 = sprintf('<input type="checkbox" name=\'People\' value="%s">',$row['Member_ID']);
}

$str = sprintf('%s,%s (%s)',$row['First'],$row['Last'],$row['Nick_Name']);
echo('<tr><td width ="10%" align="center">');
echo($str1);
echo('</td><td width="90%" align="Left">');
echo($str);
echo('</td >');
echo('</tr></td>');
}

And here is my Javascript, I've added the whole function incase it's not clear just from a code segment.


function TeamSetup()
{
var required = document.form1.BowlersPerTeam.value;
var total = document.form1.People.length;
var Tm = document.form1.TeamName.value;
var cnt = 0;
var peopleIDs = "";

var test = document.getElementsByName('People');

if(isArray(document.form1.People))
{
for(var i=0; i < total; i++)
{
if(document.form1.People[i].checked)
{
if(peopleIDs.length > 0)
{
peopleIDs = peopleIDs +",";
}
peopleIDs = peopleIDs + document.form1.People[i].value.toString();
cnt++;
}
}
}
else
{
if(document.form1.People.checked)
{
cnt=1;
peopleIDs = peopleIDs + document.form1.People.value.toString();

}
}

if(Tm.length > 0)
{
if(cnt == required || cnt > 0)
{
//Hand off to next step for completeing Processing.
var myForm = document.createElement("form");
myForm.action=window.location;
myForm.method="Post";
myForm.appendChild(createformInput("Process","PP"));
myForm.appendChild(createformInput("people",peopleIDs));
myForm.appendChild(createformInput("Team",GetQueryString("T")));
myForm.appendChild(createformInput("League",GetQueryString("L")));
myForm.appendChild(createformInput("TeamName",Tm));
document.body.appendChild(myForm);
myForm.submit();
}
else
{
alert('You need to Select at least 1 player');
}
}
else
{
alert('You need to enter the team name!');
}
}

Dormilich
06-11-2010, 01:53 PM
there are some points that could prove problematic:

isArray(document.form1.People)
depending on the definition of isArray(), that returns false, because document.form1.People is not an Array but a NodeList/HTMLCollection.


peopleIDs = peopleIDs + document.form1.People[i].value.toString();
unnecessary, formelement.value always returns a String.

a link to the page would be incredibly helpful.

xfinnerjx
06-11-2010, 02:22 PM
depending on the definition of isArray(), that returns false, because document.form1.People is not an Array but a NodeList/HTMLCollection.

Thanks for this I didn't know that isArray() wouldn't give me the result I was after.

I have changed the code to the below solution and it's working fine now.
Thanks for putting on the right path.


function TeamSetup()
{
var required = document.form1.BowlersPerTeam.value;

var Tm = document.form1.TeamName.value;
var cnt = 0;
var peopleIDs = "";

if(document.form1.People.length == undefined)
{
if(document.form1.People.checked)
{
cnt=1;
peopleIDs = peopleIDs + document.form1.People.value.toString();

}
}
else
{
var total = document.form1.People.length;
for(var i=0; i < total; i++)
{
if(document.form1.People[i].checked)
{
if(peopleIDs.length > 0)
{
peopleIDs = peopleIDs +",";
}
peopleIDs = peopleIDs + document.form1.People[i].value.toString();
cnt++;
}
}
}


if(Tm.length > 0)
{
if(cnt == required || cnt > 0)
{
//Hand off to next step for completeing Processing.
var myForm = document.createElement("form");
myForm.action=window.location;
myForm.method="Post";
myForm.appendChild(createformInput("Process","PP"));
myForm.appendChild(createformInput("people",peopleIDs));
myForm.appendChild(createformInput("Team",GetQueryString("T")));
myForm.appendChild(createformInput("League",GetQueryString("L")));
myForm.appendChild(createformInput("TeamName",Tm));
document.body.appendChild(myForm);
myForm.submit();
}
else
{
alert('You need to Select at least 1 player');
}
}
else
{
alert('You need to enter the team name!');
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum