INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Passing an array of checkbox flags to another page

Passing an array of checkbox flags to another page

(OP)
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 --> PHP

<form action="redirect.php" method="POST">
<center><table vertical-align: top>
<tr>
	<td>  </td>
	<td>  </td>
	<td>    </td>
	<td>    </td>
	<td align="center">NCR</td>
	<td>    </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>   </td>
		<td><input type="text" name="score[]" size="4" ></td>
		<td>    </td>
		<td><input type="checkbox" name="NCR[]" value="1" align="center"  /></td>
		<td>    </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

CODE --> 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-

CODE --> 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.

RE: Passing an array of checkbox flags to another page

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

RE: Passing an array of checkbox flags to another page

(OP)
I've added some count code -

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.

RE: Passing an array of checkbox flags to another page

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'];
         echo "<pre>" . print_r($_POST) . "</pre>";

	$_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

RE: Passing an array of checkbox flags to another page

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?
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
www.studiosoft.co.uk

RE: Passing an array of checkbox flags to another page

(OP)
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.

RE: Passing an array of checkbox flags to another page

Quote:


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

RE: Passing an array of checkbox flags to another page

There is a PHP forum here.

forum434: PHP

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close