Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Need Help with Getting a Checkbox Array

    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.

    Code:
    			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.

    Code:
    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!');
    	  }
    }

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    there are some points that could prove problematic:
    Code:
    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.

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

    a link to the page would be incredibly helpful.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    xfinnerjx (06-11-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    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.

    Code:
    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!');
    	  }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •