• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

PHP Checkboxes Not Reading on Submit

I have a form which contains radio boxes:

<li><label for="fd-1"><input type="checkbox" id="fd-1" value="1" name="nActionID[]" />Action option 1</label></li>

<li><label for="fd-1"><input type="checkbox" id="fd-1" value="2" name="nActionID[]" />Action Option 2</label></li>

<li><label for="fd-1"><input type="checkbox" id="fd-1" value="3" name="nActionID[]" />Action Option 3</label></li>

When I post it to the save script, and print out the variable... I get nothing.. I've tried:
print "nAction". $_POST["nActionID[]"]."<br>";
print $nActionID;
print $nActionID[0];

I can't get it.... when i change the form inputs to be singular: <input type="checkbox" name="nActionID" value="1"> then I can get the single record, but naturally I need all checked boxes.

When I submit as GET i see the following:  &nActionID%5B%5D=1&nActionID%5B%5D=2



0
phenixfilms
Asked:
phenixfilms
1 Solution
 
gamebitsCommented:
Checkboxes should have different names because they are independant from each others, if a checkbox is not checked it will not be submitted.
0
 
phenixfilmsAuthor Commented:
But then I would have do over 100 if statements to check if it were submitted. In dynamic development you are supposed to use somename[] and then php receives all the values as an array.

I created a test script and it works, i think it has to do with the fact that I'm using Fusebox framework....
0
 
phenixfilmsAuthor Commented:
http://www.fusebox.org/forums/messageview.cfm?catid=6&threadid=4298&FTVAR_MSGDBTABLE=

Theres my solution. I had to access it via $attributes
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
fcardinauxCommented:
Run this script and you will understand how the $_POST['nActionID'] array is filled: it only contains the items that have been checked.
<html><body><form method="post">
<li><label for="fd-1"><input type="checkbox" id="fd-1" value="1" name="nActionID[]" />Action option 1</label></li>
 
<li><label for="fd-2"><input type="checkbox" id="fd-2" value="2" name="nActionID[]" />Action Option 2</label></li>
 
<li><label for="fd-3"><input type="checkbox" id="fd-3" value="3" name="nActionID[]" />Action Option 3</label></li>
<input type="submit">
</form>
<?php
echo '<pre>';
var_dump($_POST);
echo '</pre>';
?>
</body></html>

Open in new window

0
 
fcardinauxCommented:
In addition to this, you need to give a different id to each checkbox, and therefore a different "for" value to each label.
0
 
icareyCommented:
Each value will be passed as an array as indicated by [] in name=nActionID[]

when you use action POST the array will arrive as $_POST["nActionID"] you don't need the [] here as you have above.

the array will have 2 values the array key and the value
eg
array key value
      0            1
      1            2
      2            3

All that is needed now is to loop through the array and get the values this can be done by the list function


<form action="action.php" method="POST">
 
<li><input type="checkbox" id="fd-1" value="1" name="nActionID[]" />Action Option 1</li>
 
<li><input type="checkbox" id="fd-1" value="2" name="nActionID[]" />Action Option 2</li>
 
<li><input type="checkbox" id="fd-1" value="3" name="nActionID[]" />Action Option 3</li>
<br />
 
<input type="submit" name="SUBMIT" value="SUBMIT" />
</form>
 
if (empty( $_POST["nActionID"]))
{
	echo "No action Selected"; 
}
else
{
	while (list ($actionkey, $actionval) = each   ($_POST["nActionID"]))
		{
			print "nAction ". $actionval."<br>";               
        }
 
}

Open in new window

0
 
fcardinauxCommented:
This is an improvement of the code in my previous comment. Both codes are tested.

<html><body><form method="post">
<li><label for="fd-1"><input type="checkbox" id="fd-1" value="1" name="nActionID[]" />Action option 1</label></li>
 
<li><label for="fd-2"><input type="checkbox" id="fd-2" value="2" name="nActionID[]" />Action Option 2</label></li>
 
<li><label for="fd-3"><input type="checkbox" id="fd-3" value="3" name="nActionID[]" />Action Option 3</label></li>
<input type="submit">
</form>
<?php
if ( empty($_POST['nActionID'])) echo 'Please select an action';
else {
    foreach ($_POST['nActionID'] as $action_id) echo "<br>Action $action_id selected";
}    
?>
</body></html>

Open in new window

0
 
fcardinauxCommented:
You recommend to close the solution as "PAQ'd with a full refund". Moreover the notice says that you have "requested that this question be closed with a full refund and storing the question in our solution database".

If there is a previously asked question (PAQ) on this subject, why store this solution in your database? Why not simply delete it?

If you don't delete it, refunding the points is unfair because icarey's answer was pertinent and because I provided an answer that was tested and worked. The fact that a similar question has already been answered shouldn't cancel out our efforts to help this user. This website is a place where users get help from experts throught exchanges, not a place where competitors race to be the first and only one to answer a specific question. You ask the experts to be patient and not to abandon the users, not to rush and ignore them if they ask a "PAQ". You even write at the bottom of each of your messages: "Please remember that it is important to return to the question you are participating in".

To encourage this behaviour, may I ask you to split the points between icarey and me?

Thank you for your patience.  




0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now