Solved

Obtaining array in POST variable

Posted on 2015-02-19
4
182 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 82

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 42

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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 look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now