Solved

Using javascript   document.forms to catch checkbox[ ] values - only catches one value not all checked items

Posted on 2008-11-03
6
932 Views
Last Modified: 2012-08-13
Hi everyone hope you are all well.
Guys i have a problem with my php page.

I have a form that has a checkbox with name 'checkbox[]' so im trying to capture an array of checked items on the page.

Im trying to use

document.forms[0].submit();

which ONLY captures one selected checkbox and not all.

Can anyone suggest how to capture all selected checkbox items?

Thank you.

The reason i want to use javascript is that I wish to create a javascript function as follows:

function doit()
{
document.forms[0].submit();
document.forms[1].submit();
document.forms[2].submit();

}

since I have 3 forms on the same page, and i wish to submit the values to the same receiving page to grab all the checkbox items.
The reason im using 3 forms (and not one) is because data is being returned from a mysql table, but not all rows will have data posted to the receiving page, so my page is split up according to if else conditions.

Any help on this greatly appreciated.
      
0
Comment
Question by:Simon336697
  • 3
  • 2
6 Comments
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 60 total points
ID: 22866532
PHP uses checkboxes as an array.  Did you code for that in the javascript?  

You can see here:

http://www.php.net/manual/en/faq.html.php#faq.html.select-multiple
0
 
LVL 11

Expert Comment

by:NoiS
ID: 22866705
Maybe...

var form = document.getElementById('form_id');
var checks = formgetElementsByTagName('input');
 
for (var i=0;i<checks.lenght;i++){
  if(checks[i].type='checkbox'){
     // the element is a checkbox
     if(checks[i].checked==true){
        // the checkbox is checked
     }
     if (checks[i].name.indexOf('checkbox')>-1){
        // the checbox have checkbox in the name...
     }
  }
}

Open in new window

0
 
LVL 11

Accepted Solution

by:
NoiS earned 440 total points
ID: 22866766

Just a comment. Often I disagreed in use more than one form on the page. Rarely this thing is necessary.
I think more usefull to do something like this

<form>
// For address fields
   <input type="type" name="address[city]" />
   <input type="type" name="address[state]" />
   <input type="type" name="address[zip]" />

// For other stuff
   <input type="type" name="other[something]" />
   <input type="type" name="other[something]" />
</form>

On PHP..

<?php
   $address = isset($_REQUEST['address'])?$_REQUEST['address']:NULL;
   $other = isset($_REQUEST['other'])?$_REQUEST['other']:NULL;
?>

Another way is use submit buttons
 <input type="submit" name="action" value="Delete" />
 <input type="submit" name="action" value="Add" />
 <input type="submit" name="action" value="Update" />

When You click the submit button Add only 'Add' value will be on $_REQUEST['action'] so you can control what to do
<?php
$action = isset($_REQUEST['action'])?strtolower($_REQUEST['action']):NULL;

switch($action){
   case 'add':
      // ADD code
    break;

   case 'delete:
      // Delete code
    break;
   // and so on
}
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Simon336697
ID: 22866815
Hi Brad and Nois....guys thank you SO VERY MUCH for your really great help.
Im looking into it.
Just for your guys info, the reason im using >1 form.......please look at my screen dump of the page.
Not every row returned from the mysql table has data that will be posted using checkboxes....note that black rows with no checkbox.....im using if else statements to color code rows and also to not include checkboxes for those different color coded rows based on the value of a field in a mysql table.

Im going to try your suggestions, as well as hack my css (time consuming because the presentation i have right now is perfect....but to get down to one form is going to be a css nightmare).
page.jpg
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22866841
Guys here is my script.....
<head>
	
<script language="javascript">
<!--
function doit()
{
document.forms[0].submit();
 
}
-->
</script>
 
</head>
 
 
<?php
 
 
/**********************************************************************
Define the page number so we know what page we are on:
***********************************************************************/
$pageNumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
 
/**********************************************************************
Define the number of results to show per page:
**********************************************************************/
$perPage = 270;
 
/**********************************************************************
Define our PADDING VALUE: eg. "..." is 3, "......." is 7
**********************************************************************/
$padding = 10;
 
/**********************************************************************
Get the starting index of our results: (on a per page basis)
eg. $startIndex = ( 3 * 10 ) - 10 = 20   (on page 3, 20 is start index)
**********************************************************************/
$startIndex = ($pageNumber * $perPage) - $perPage;
 
/**********************************************************************
Get the total number of database records:
**********************************************************************/
// $totalCount = "SELECT COUNT(*) as 'Total' FROM technologies_tek";
$totalCount = "SELECT COUNT(*) as 'Total' FROM entries_ent";
$rsCount = mysql_query($totalCount, $conn) or die(mysql_error());
$rowCount = mysql_fetch_object($rsCount);
// echo mysql_result($rsCount,0); // this works and gives a count of 10.
 
/**********************************************************************
List the page numbers/listings here:
**********************************************************************/
 
?>
 
 
	
<?php	
 
// print "<div align=\"center\">"; // ------------------------------------------------------------------- DIV holding navigation:
	
	// GET OUR TOTAL NUMBER OF PAGES:
	// --------------------------------
	$numofPages = ceil($rowCount->Total / $perPage);
	// print $numofPages; // this gives total pages:
	// eg. 500 rows / 10 = 50 pages.
	
	// PRINT LINK TO FIRST PAGE:
	// --------------------------------	
	print "<div id=\"div_first\">";
	print "<a href=\"delete_entry.php?page=1\">FIRST</a>&nbsp;&nbsp;&nbsp;";
	print "</div>";
	
	
	// print "<a href=\"#\" onclick=\"resetVariable()\">Reset</a>";	
	
 
	// PRINT START AND END RECORD NUMBERS FOR CURRENT PAGE:
	// --------------------------------		
	print "<div id=\"div_start2end\">";	
	echo "Records: " . ($startIndex + 1) . " to " . ($startIndex + 1 + 269);
	print "</div>";
	
	// PRINT A PREVIOUS LINK IF CURRENT PAGE IS NOT THE LAST PAGE:
	// --------------------------------		
	if ($pageNumber > 1) {
		print "<div id=\"div_previous\">";		
		print "<a href=\"delete_entry?page=".($pageNumber - 1)."\">PREVIOUS</a>&nbsp;&nbsp;&nbsp;";		
		print "</div>";	
	}
	
	/*********************************************************************************************
	TAKE CARE OF EVERYTHING TO THE LEFT OF THE CURRENT PAGE: <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	*********************************************************************************************/
	
	// PRINT THE LOWER LIMIT '...' dots:
	// --------------------------------	
	// If our current page, minus the padding, is > 1 (eg. page 1), then PRINT ...
	// to indicate that there are more pages before the padding value, eg.
	// FIRST ... 10 11 12 13 14 <15> 
	// If our current page, minus our padding, is greater than 1:
	if (($pageNumber - $padding) > 1)  {
		print "&nbsp;...&nbsp;";
		// Set our lower limit:
		$lowerLimit = $pageNumber - $padding;
		// print $lowerLimit;
	
	// PRINT ALL THE PAGES BETWEEN THE LOWER LIMIT AND THE CURRENT PAGE:
	// --------------------------------	
	// eg. if current page is 7, and lower limit is 2, then
	// we need to print all pages 3, 4, 5 and 6.
	// Print all padded numbers between lower limit and current page
		for ($i = $lowerLimit; $i < $pageNumber; $i++) {
			print "&nbsp;&nbsp;&nbsp;<a href=\"delete_entry.php?page=".$i."\">".$i."</a>&nbsp;&nbsp;&nbsp;";	
		}
		// If we take away the padding, which puts us down to page 1, what do we do
		// if we are not on the first page, but maybe on page 3, where no padding applies?
		// Without the following, all we would get is 'FIRST LAST'
	} else {
		for ($i = 2; $i < $pageNumber; $i++) {
			print "<a href=\"delete_entry.php?page=".$i."\">".$i."</a>&nbsp;&nbsp;&nbsp;";			
		}
	}
	
	/*********************************************************************************************
	###########################################
	TAKE CARE OF WHAT PAGE WE ARE CURRENTLY ON:
	###########################################
	*********************************************************************************************/		
 
	// If we're not on the first page, or the last page, print current page:
	
	if (($pageNumber !=0) && ($pageNumber !=1) && ($pageNumber !=$numofPages)) {
		print "<b> - " . $pageNumber . " - </b>";	
	}
	
	/*********************************************************************************************
	TAKE CARE OF EVERYTHING TO THE RIGHT OF THE CURRENT PAGE: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	*********************************************************************************************/	
 
	// PRINT THE UPPER LIMIT '...' dots:
	// --------------------------------	
	// If our current page, plus our padding, is less than the total number of pages:
	if (($pageNumber + $padding) < $numofPages) {
		// Set upper limit:
		$upperLimit = $pageNumber + $padding;
		// Print all numbers from padded pages above current page:
		for ($i = ($pageNumber + 1); $i <= $upperLimit; $i++) {
			print "&nbsp;&nbsp;&nbsp;<a href=\"delete_entry.php?page=".$i."\">".$i."</a>&nbsp;&nbsp;&nbsp;";	
		}
		print "&nbsp;...&nbsp;";		
	} else {
		// Print all page numbers between number of pages and current page:
		for (($i = $pageNumber + 1); $i < $numofPages; $i++) {
			print "<a href=\"delete_entry.php?page=".$i."\">".$i."</a>&nbsp;&nbsp;&nbsp;";	
		}		
	}
 
	// PRINT A NEXT LINK IF CURRENT PAGE IS NOT THE LAST PAGE:
	// --------------------------------	
	if ($pageNumber < $numofPages) {
		print "<div id=\"div_next\">";			
		print "<a href=\"delete_entry.php?page=".($pageNumber + 1)."\">NEXT</a>&nbsp;&nbsp;&nbsp;";	
		print "</div>";							
	}
	
	// PRINT THE NUMBER OF RECORDS IN TOTAL:
	// --------------------------------		
	print "<div id=\"div_totalrecords\">";	
	echo "&nbsp;Total Records: " . mysql_result($rsCount,0) . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pages: " . $numofPages;
	print "</div>";
	
	// PRINT LINK TO LAST PAGE:
	// --------------------------------	
	print "<div id=\"div_last\">";	
	print "&nbsp;&nbsp;&nbsp;<a href=\"delete_entry.php?page=".$numofPages."\">LAST</a>";	
	print "</div>";	
	print "</div>"; // ----------------------------------------------------------------------------------- end of div_navigation_top_row_wrapper:
 
 
/**********************************************************************
Get page results, and LIMIT by the startIndex:
**********************************************************************/
 
/*
$sql = "SELECT id_tek, name_tek
		  FROM technologies_tek ORDER BY id_tek
		  LIMIT $startIndex, $perPage";
*/
 
/*
$sql = "SELECT id_ent, name_ent, image_ent, header_cat_ent
		  FROM entries_ent order by fk_id_cat_ent, header_cat_ent, name_ent asc
		  LIMIT $startIndex, $perPage";		
*/
		  
 
$sql = "select name_ent, name_cat, header_cat_ent, image_ent, id_ent, code_ent from
		categories_cat, entries_ent
	   where fk_id_cat_ent = id_cat
	   order by name_cat, header_cat_ent asc
	   LIMIT $startIndex, $perPage";	  
	  
		    
		  
/**********************************************************************
Get the result set:
**********************************************************************/		  
$rs = mysql_query($sql, $conn) or die(mysql_error());
 
/**********************************************************************
Do we have results?:
**********************************************************************/			  
if (mysql_num_rows ($rs) > 0) {
	// print "You have rows!";
?>	
		<div id ="div_results_wrapper"> <!-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> div_results_wrapper: -->	
<?php		
	$x = 1;
	
		while ($row = mysql_fetch_object($rs)) {	
			
			// $hiya = $row->id_ent;
			
			// DEFINE ALTERNATING ROW STYLE CLASSES
			// -------------------------------------
			$header = "headerclass";
			$class1 = "noncolumns1";
			$class2 = "noncolumns2";
			$last_chr = substr ( $row->header_cat_ent, -1 );					
									
				if ($class == $class1) {
					$class = $class2;
				} 
				else {
				$class = $class1;
				}
				
					// START OF DETERMINING ROW COLOR AND ALTERNATING ROWS IF-ELSE STATEMENTS
					// ----------------------------------------------------------------------
					// If the first record is record 1, AND it is a header row (eg. ends with '0'), then do this.
					if($x == 1 && $last_chr == "0"){
						echo '<div class="column">';
						?>
							<div class="<?php echo $header; ?>">							
									<?php	
									echo $row->name_ent;	
									?>									
							</div>	
															
						<?php
							$x++;
					}
					
					// If the first record is record 1, AND it is NOT a header row, then do this.
					elseif($x == 1 && $last_chr !== "0"){
						echo '<div class="column">';
						?>
						<div class="top_row">
								<div id="div_nameholder">
									<?php	
									echo $row->name_ent;	
									?>																		
								</div>													
						</div>
							<div class="checker">
								<form name="formall_delete" id="formall_delete" method="post" action="delete_entry_ac.php">		
									<input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $row->id_ent; ?>">	 
								</form>								
							</div>							
						<?php
							$x++;
					}					
					
					
					elseif($x == 45 && $last_chr == "0"){
						?>
							<div class="<?php echo $header; ?>">							
									<?php	
									echo $row->name_ent;	
									?>									
							</div>	
															
					</div> <!-- end of div class = column -->
						<?php
						$x=1;	
					}											
											
					elseif($x == 45 && $last_chr !== "0"){	 
						?>
						<div class="bottom_row">
 
								<div id="div_nameholder">
									<?php	
									echo $row->name_ent;	
									?>									
								</div>								
						</div>
							<div class="checker">
								<form name="formall_delete" id="formall_delete" method="post" action="delete_entry_ac.php">		
									<input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $row->id_ent; ?>">	 
								</form>								
							</div>							
					</div> <!-- end of div class = column -->
						<?php
						$x=1;	
					}	
					
							
					
					else {
 
					// If any row other than the top or bottom row has a header_cat_ent of '0', then style the record with the $header style..
					if( $last_chr == "0" ) {
						?>
						<div class="<?php echo $header; ?>">	
									<?php								
									echo $row->name_ent;	
									?>
						</div>	
						<?php
						$x++;		
					}						
						
						
						
					if( $last_chr == "1" ) {
						?>
						<div class="<?php echo $class; ?>">	
 
								<div id="div_nameholder">
									<?php	
									echo $row->name_ent;	
									// function wz_tooltip_image ($displaynamelink, $title, $image)																																		
									// print $row->name_ent;
									// print ": ";
									?>									
									
								</div>
							
 
						</div>
						<div class="checker">
								<form name="formall_delete" id="formall_delete" method="post" action="delete_entry_ac.php">		
									<input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $row->id_ent; ?>">	 
								</form>							
						</div>		
						<?php
						$x++;		
					}
					
					
					
					
					}		// end of last else statement						
		} // end of while statement
		
	
}
else {
	print "Sorry, no results found.";
}		  
		  
?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31512650
Thanks so much to both of you.
Ill do some more reading really appreciate your help :>)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question