Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an array of checkbox flags to another page

Status
Not open for further replies.

jjack46

Programmer
Jul 29, 2013
10
AU
I have a page where I enter updates to multiple records (up to a hundred) on the same page. The entries are put into an array which is passed by post to a processing page which uses PHP. This works okay for text box entries. However, I want to put a checkbox for each entry so I can pass a flag to the database. I've tried using an array of checkboxes but it doesn't seem to work.
This is the code:
Code:
<form action="redirect.php" method="POST">
<center><table vertical-align: top>
<tr>
	<td>&nbsp;&nbsp;</td>
	<td>&nbsp;&nbsp;</td>
	<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	<td align="center">NCR</td>
	<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	<td align="center">Disq</td>
</tr>	
<? 
while ($row = mysqli_fetch_assoc($res))
{
		$playerId = $row['PlayerNo'];
		$handicap = $row['Handicap'];
	 ?>
	<tr>
		<td><input type="hidden" name="playerNo[]" value="<? echo $playerId ?>"  /></td>
	</tr> 
	<tr>
		<td><input type="hidden" name="handicap[]" value="<? echo $handicap ?>"  /></td>
	</tr> 
	<tr>
		<td><?php echo $row['PreferredName']." ".$row['Surname']."   "
			.$row['StateRep']."  ".$row['Handicap'] . "\n"; ?></td>
		<td>&nbsp;&nbsp;&nbsp;</td>
		<td><input type="text" name="score[]" size="4" ></td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td><input type="checkbox" name="NCR[]" value="1" align="center"  /></td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td><input type="checkbox" name="disq[]" value="1" align="center"  /></td>
	</tr>
	<?php	
}
	
?>
	
</table></center>
<br /><br />
<center><input type="submit" name="addScores" value="Save scores"  /></center>
<br /><br />
<center><input type="submit" name="addScores" value="Cancel" /></center>
</form>
This is the section of the code that collects and sends the data. The 'NCR[]' and 'Disq[]' are what I trying to pass to the processing page via the 'redirect' page. The PlayerNo, Handicap and Scores arrays work okay.
It uses the following code to pass from the 'redirect' to the processing page. This a section of the 'redirect.php' page

PHP:
elseif (trim($_POST['addScores']) == 'Save scores')
{
	$_SESSION["plNumber"] = $_POST['playerNo'];
	$_SESSION["handicap"] = $_POST['handicap'];
	$_SESSION["score"] = $_POST['score'];
	$_SESSION["ncr"] = $_POST['NCR'];
	$_SESSION["disq"] = $_POST['disq'];
		
	header("location: processScores.php");
	exit;
}

When it gets to the processing page the first part is the this-

PHP:
session_start();
	
	$plNumber = $_SESSION["plNumber"];
	$scores = $_SESSION['score'];
	$scoreDay = $_SESSION['scoreDay'];
	$scoreTime = $_SESSION['scoreTime'];
	$handicap = $_SESSION['handicap'];
	$ncrArray = $_SESSION['ncr'];
	$disqArray = $_SESSION['disq'];
	$x = count($plNumber);
	for($i=0; $i < $x; $i++)
        {
    	echo("Disq = ".$disqArray[$i] . "\n");
        }

I put the loop in to show me what was coming across into the $disqArray, which is basically nothing.

Any suggestions.
 
1. Are you checking any of the checkboxes? By default unchecked checkboxes do not get submitted, so if you had say 10 checkboxes and you only checked 2, your array would be of 2 items. If you check nothing, then you get nothing.

2. What does $x equal? That should give you an idea of how many items you are getting back.

i.e.
Code:
$x = count($plNumber);

echo "X equals: " . $x;

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I've added some count code -
PHP:
$x = count($disqArray);
echo "X equals: " . $x;
and checked 3 of the boxes. The count is 0 it should be 20. If checkboxes only return a result if they are checked it should still be giving a count of 3. If I count the $plNumber array I get 20 which is correct. Using the checkboxes would have been a neat way of doing what I wanted but if I can't get a negative result returned I may have to find a work around.
 
As I said, unchecked checkboxes do not get submitted, because otherwise there would be no way to know if they are checked or not. So only the checked ones get submitted.

Its never going to be 20 unless all 20 are checked. And its never going to be negative numbers. its 0 if there's nothing checked, or the amount of checked checkboxes. But you won't really know what row they belong to.


Start by stepping through your code to figure out why its empty.

I would print out what I'm getting before saving it in the session variable:

Code:
elseif (trim($_POST['addScores']) == 'Save scores')
{
	$_SESSION["plNumber"] = $_POST['playerNo'];
	$_SESSION["handicap"] = $_POST['handicap'];
	$_SESSION["score"] = $_POST['score'];
	$_SESSION["ncr"] = $_POST['NCR'];
[tab][tab] [b]echo "<pre>" . print_r($_POST) . "</pre>";[/b]

	$_SESSION["disq"] = $_POST['disq'];

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I am not familiar with this PHP syntax but I would like to make a couple of observations.
Does
Code:
<td><input type="checkbox" name="[b]NCR[][/b]" value="1" align="center"  /></td>
assign each checkbox with a different name/array number?
Why not create multiple checkboxes with different names and process them in a recursive loop rather than put them in an array. The recursive loop would allow you to identify which checkboxes were selected.


Keith
 
I've decided to use a drop down box with 3 options (Ok, NCR & Disq) then I can put the value of the one selected into an array. It looks good and works okay.

Many thanks for your replies.
 
I am not familiar with this PHP syntax but I would like to make a couple of observations.
Does
CODE
<td><input type="checkbox" name="NCR[]" value="1" align="center" /></td>
assign each checkbox with a different name/array number?

Yep, that's exactly what it does. It creates a PHP array with the checked checkboxes.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top