Obtaining array in POST variable

Hello,

I'm trying to find a way to obtain a subset array from the passed POST array in a php function.  Right now my html form has one input into which a number is typed.  Based on that value a number of checkboxes will appear after it.  What would be the best way to obtain the array of checkbox values from the POST?  I am submitting the form using AJAX and .serializeArray() for the form.  Would it be best just to copy the array, trimming out the first entry, which is the first input value?  So if the POST was: input=4 cb1=1 cb2=0 cb3=0 cb4=1, just create a separate array starting at index 1?  If anyone has any ideas or opinions please let me know.
Thanks,
William-BAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
You should first be aware that checkboxes that are Not checked... are Not submitted.  So if 'cb2' and 'cb3' are not checked, you will not see them in the POST array.  I have a simple program that you can use to see what actually gets POSTed.  You will need to create a simple test page with your form and without AJAX and use it as the 'action' page to see what happens.

postdump.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>POST Dump</title>
</head>
<body>
<pre>
<?php 
var_dump($_POST);
foreach($_POST as $key => $value) {
    echo $key.' = '.$value."<br>";
}

?>
</pre>
</body>
</html>

Open in new window

0
 
Chris StanyonConnect With a Mentor Commented:
The way I'd work this is to use the <input> value to loop through all the checkboxes, whether they're submitted or not and build an array in a loop. Easiest way to do this would be to name your checkboxes using an indexed array:

<form action="data.php" method="post">
	<input type="text" name="qty" value="" /><br>
	<input type="checkbox" name="cb[0]" value="1">Check 1<br>
	<input type="checkbox" name="cb[1]" value="1">Check 2<br>
	<input type="checkbox" name="cb[2]" value="1">Check 3<br>
	<input type="checkbox" name="cb[3]" value="1">Check 4<br>
	<input type="submit" value="Submit" />
</form>

Open in new window


Then in your php script (data.php), you know how many checkboxes you've got and what they're called, so just loop through them. If they were ticked, they will be set - unticked and they won't be:

$qty = $_POST['qty'];
for ($i=0; $i<$qty; $i++) { 
	$checks[$i] = ( isset($_POST['cb'][$i]) ) ? 1 : 0;
}

Open in new window


The $checks array will now contain 1 for each checkbox that was ticked and 0 for those that weren't.
0
 
William-BAuthor Commented:
Thanks a lot guys, I really liked Ray's article and both Dave and Chris brought some issues that I hadn't considered to my attention.  I might end up using the hidden control method, just because using the array ID's it should be relatively easy to implement, that way I can have the values of all the controls.  Thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.