Solved

Obtaining array in POST variable

Posted on 2015-02-19
4
186 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 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 109

Accepted Solution

by:
Ray Paseur earned 167 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

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.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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