Solved

PHP Checkboxes Not Reading on Submit

Posted on 2008-10-19
11
618 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
ID: 22754666
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
ID: 22754729
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
ID: 22754890
http://www.fusebox.org/forums/messageview.cfm?catid=6&threadid=4298&FTVAR_MSGDBTABLE=

Theres my solution. I had to access it via $attributes
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 8

Expert Comment

by:fcardinaux
ID: 22754974
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
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22754985
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
ID: 22755523
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
ID: 22755756
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
ID: 24120845
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

785 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