?
Solved

Obtaining array in POST variable

Posted on 2015-02-19
4
Medium Priority
?
192 Views
Last Modified: 2015-02-20
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,
0
Comment
Question by:William-B
4 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 668 total points
ID: 40620183
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
 
LVL 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 664 total points
ID: 40620226
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 668 total points
ID: 40620368
0
 

Author Closing Comment

by:William-B
ID: 40621782
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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