[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Obtaining array in POST variable

Posted on 2015-02-19
4
Medium Priority
?
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 44

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

650 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