Reputation: 13
I'm new to PHP and having a problem reading checkboxes submitted by a form. Before explaining it i would like to mention that i'm trying to edit a much larger application and the data cannot be sent in any other way. It's just a matter of finding a good method of reading what is being sent.
When a normal text input is sent, the form will post the following:
custom[0][type]="text"
custom[0][name]="VariableName"
custom[0][value]="VariableName"
Basically there is a main "custom" multidimensional array that has several elements (0,1,2,3 etc) and each element has name and value.
However, when one of the elements is a checkbox, the following stuff gets posted:
custom[1][type]="list"
custom[1][name]="SelectedOptions"
custom[1][value]="Value1"
custom[1][value]="Value3"
custom[1][value]="Value5"
Getting to the PHP side of things, this is the code i'm using to read the data sent by the form. The code below works ok in scenario 1 (with text based inputs) but only reads one value when we have list type custom data.
foreach($_POST['custom'] as $item){
if($item['value'] != "") echo $item['name'].'='.$item['value']
}
The problem is that $item['value'] only reads one of the values, not all 3. How can i get all 3 values in a variable ? It probably is a very easy thing...
To put it all combined, this is what it is sent with POST (3 checkboxes are checked for Variable2)
custom[0][name] Variable1
custom[0][type] text
custom[0][value] ValueForVariable1
custom[1][name] Variable2
custom[1][type] checkbox
custom[1][value] Value1
custom[1][value] Value3
custom[1][value] Value5
And this is what print_r($_POST) shows for the posted data above
[custom] => Array
(
[0] => Array
(
[value] => ValueForVariable1
[name] => Variable1
[type] => text
)
[1] => Array
(
[value] => Value1
[name] => Variable2
[type] => checkbox
)
Just to be sure we're all on the same page, the actual data is generated by a more complex system and we can't really change that. I'm interested in seeing how we can read all 3 values for Variable2 that are sent in the POST.
Thanks !
Upvotes: 1
Views: 3198
Reputation: 13766
EDIT: With the extra information that you've since provided, I see that I originally misunderstood the problem.
Since you have no control over over the form that sends the data to your PHP script, and thus can't change it to ensure that the later checkboxes with the same name do not overwrite the earlier ones, you'll have to get access to and process the raw post data itself.
$postdata = file_get_contents("php://input");
echo $postdata;
... will output the postdata just like a GET querystring: blah=1&blah=2&blah=3
(blah indicates 3 form fields with the same name blah, the first two of which would be overwritten in $_POST
leaving $_POST['blah'] = 3
).
with a little exploding on &
and looping and parsing looking for the variables in question, or even any conflicting variables, will get you where you're trying to go.
Original Answer:
HTML forms only submit checkboxes (or radio buttons) that have been checked. If they haven't been checked, the browser won't send the data back to the server.
The main way to solve this is to know what you're looking for on the back end and test for it (i.e. if (isset($_POST['checkboxname'])
).
If you truly need a general purpose backend that will dynamically incorporate all checkbox elements, checked or not, the way I've solved this in the past is to use javascript to record all form elements on the page and submit that info with the rest of the form (and, in my case, also submit whether that field was changed or not).
Here's a function that you can run at the top of the script to treat POST the way you expect it to be treated from your ASP background:
function post_process() {
$rawpostdata = file_get_contents('php://input');
if (!$rawpostdata) return;
$fields = explode('&', $rawpostdata);
$post = array();
foreach ($fields as $field) {
list($key, $val) = explode('=', $field);
if (isset($post[$key])) $post[$key] .= ','.$val;
else $post[$key] = $val;
}
$_POST = $post;
}
Upvotes: 2