Problem retrieving values from checkboxes or radio buttons in php
I have a form that contains a section of checkboxes and another section of radio buttons. I'm having a similar problem with both.
The html for the section of checkboxes looks like:
<input type="checkbox" name="activity[]" value="run" id="run" /><label for="run">Run</label>
<input type="checkbox" name="activity[]" value="swim" id="swim" /><label for="swim">Swim</label>
<input type="checkbox" name="activity[]" value="bike" id="bike" /><label for="bike">Bike</label>
When I submit the form, the activity array does not seem to be correctly defined.
When I process the form, assuming the first two checkboxes are ticked, when the php executes
$activity = $_POST['activity'];
var_dump($activity);
it prints:
array(2) [0]=> string(0) "" [1]=> string(0) ""
Obviously it is detecting that two of the three boxes are ticked, but it is not passing through the values. If the HTML starts with the checked attribute for all checkboxes then it correctly sends through the checkbox value.
Similarly, with radio buttons, I cannot get a value to go thro开发者_如何学Pythonugh other than the default value. If the user changes the value from the default, then the parameter exists, but the value is an empty string.
It is my understanding that the array should contain the values defined for each of the boxes that was ticked, regardless of whether the box was ticked when the form loaded. What am I doing wrong?
I can't duplicate your behavior with this simple test
<?php
if ( isset( $_POST['activity'] ) )
{
$activity = $_POST['activity'];
var_dump($activity);
}
?>
<form method="post">
<input type="checkbox" name="activity[]" value="run" id="run" /><label for="run">Run</label>
<input type="checkbox" name="activity[]" value="swim" id="swim" /><label for="swim">Swim</label>
<input type="checkbox" name="activity[]" value="bike" id="bike" /><label for="bike">Bike</label>
<input type="submit" value="go" />
</form>
I clicked "swim" and "bike" and my output looked like this
array(2) { [0]=> string(4) "swim" [1]=> string(4) "bike" }
So perhaps something else is amiss that you haven't included in your question.
Even when I inspect the request with Firebug I see the expected output
Content-Type: application/x-www-form-urlencoded
Content-Length: 39
activity%5B%5D=swim&activity%5B%5D=bike
It works as expected for me, copy-pasted just like that.
Does it really not work for you, if you isolate this bit like so:
<form action="test.php" method="post">
<input type="checkbox" name="activity[]" value="run" id="run" /><label for="run">Run</label>
<input type="checkbox" name="activity[]" value="swim" id="swim" /><label for="swim">Swim</label>
<input type="checkbox" name="activity[]" value="bike" id="bike" /><label for="bike">Bike</label>
<input type="submit">
</form>
<?php
$activity = $_POST['activity'];
var_dump($activity);
?>
I suspect that you didn't isolate this issue (like I did in this snippet) and it lies somewhere else. Could that be right?
Sadly, the problem had nothing to do with what I thought! Thanks to the suggestions of Peter and Rubin I was able to determine that some javascript that I had written (ages ago) to validate the form before it was sent to the server was erroneously blanking out the values of the checkboxes and radio buttons when they got focus. A simple test to confirm the input field was a text area stopped that from happening!
精彩评论