Solved

PHP Checkboxes Not Reading on Submit

Posted on 2008-10-19
11
613 Views
Last Modified: 2013-12-13
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
Comment
Question by:phenixfilms
11 Comments
 
LVL 28

Expert Comment

by:gamebits
Comment Utility
Checkboxes should have different names because they are independant from each others, if a checkbox is not checked it will not be submitted.
0
 
LVL 1

Author Comment

by:phenixfilms
Comment Utility
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
 
LVL 1

Accepted Solution

by:
phenixfilms earned 0 total points
Comment Utility
http://www.fusebox.org/forums/messageview.cfm?catid=6&threadid=4298&FTVAR_MSGDBTABLE=

Theres my solution. I had to access it via $attributes
0
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
In addition to this, you need to give a different id to each checkbox, and therefore a different "for" value to each label.
0
 
LVL 3

Expert Comment

by:icarey
Comment Utility
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
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
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
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

13 Experts available now in Live!

Get 1:1 Help Now