364,088 Members | 5545 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

multiple checkbox

mudgilgaurav
P: 7
Hi

I have multiple checkboxes on my page and i want to access only the selected checkbox on the next page can any one give me the idea how to do it

Thnx in advance

Bye
Dec 11 '06 #1
Share this Question
Share on Google+
32 Replies


bishwadeep
P: 12
Please see the following example. Hope it will help you
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <form action="checkbox.php" method="post">
  3.   <input type="checkbox" name="checkbox" value="a">
  4.   <input type="checkbox" name="checkbox" value="b">
  5.   <input type="checkbox" name="checkbox" value="c">
  6.   <input type="checkbox" name="checkbox" value="d">
  7.   <br>
  8.   <br>
  9.   <input type="submit" name="Submit" value="Submit">
  10. </form>
  11. <?
  12.     if(isset($_POST['Submit']))
  13.     {
  14.         echo $_POST['checkbox'];
  15.     }
  16. ?>
  17. </body>
Dec 11 '06 #2

mudgilgaurav
P: 7
Please see the following example. Hope it will help you
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <form action="checkbox.php" method="post">
  3.   <input type="checkbox" name="checkbox" value="a">
  4.   <input type="checkbox" name="checkbox" value="b">
  5.   <input type="checkbox" name="checkbox" value="c">
  6.   <input type="checkbox" name="checkbox" value="d">
  7.   <br>
  8.   <br>
  9.   <input type="submit" name="Submit" value="Submit">
  10. </form>
  11. <?
  12.     if(isset($_POST['Submit']))
  13.     {
  14.         echo $_POST['checkbox'];
  15.     }
  16. ?>
  17. </body>
HI

Thnx for ur response
this thing know already i asked if i select 3 checkboxes on the first page then how to access all those three selected check boxes the method u gave print only the last selected box

if u know then plz reply

bye
Dec 11 '06 #3

ronverdonk
Expert 2.5K+
P: 4,047
HI

Thnx for ur response
this thing know already i asked if i select 3 checkboxes on the first page then how to access all those three selected check boxes the method u gave print only the last selected box

if u know then plz reply

bye
This sample is incorrect! It treats the checkboxes like radio buttons because you give them all an identical name. And only one of the names can be set that way. When you want to get all the values that have been checked, you make the name field an array. So when you click checkboxes 1 and 3 (values 'a' and 'c'), you have an array that, when printed using print_r looks like:
Expand|Select|Wrap|Line Numbers
  1. Array ( 
  2.        [0] => a 
  3.        [1] => c 
  4.       ) 
So in your catching script you must process the array with name $_POST['checkbox']
Here is the code:
[php]<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="a">
<input type="checkbox" name="checkbox[]" value="b">
<input type="checkbox" name="checkbox[]" value="c">
<input type="checkbox" name="checkbox[]" value="d">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?

/* and in your checkbox.php you do this: */

if(isset($_POST['Submit']))
{
for ($i=0; $i<count($_POST['checkbox']);$i++) {
echo "<br />value $i = ".$_POST['checkbox'][$i];
}
}
?>
</body>
[/php]
When you select boxes 2 and 4, the result of this script is then
Expand|Select|Wrap|Line Numbers
  1. value 0 = b
  2. value 1 = d
Ronald :cool:
Dec 11 '06 #4

crisis123
P: 18
hi i have multiple checkboxes on my page and i wanna select only one at a time...i dont want to use radio buttons.

can anyone provide some help??

thanku
Jun 18 '09 #5

Dormilich
Expert Mod 5K+
P: 6,604
but radio button would be best suited for that (i.e. they allow only one to be checked in a group)

anything beyond that requires JavaScript. (you can make it, so that you can select only one checkbox, but if the user disables JavaScript…)
Jun 18 '09 #6

crisis123
P: 18
thanks a lot
well i had been askd to use the checkbox by the end user..thats why i had this problem.

another problem is that i have used <input type="checkbox" name="yes[]" value="Y"/> for creating the checkbox
but when i create a variable $checkbox=$_POST['yes'] in my php script i get an error of undefined index
i have used post method to create the form

any suggestions?
Jun 18 '09 #7

crisis123
P: 18
sorry i have used $checkbox=$_POST['yes[]'] nd still encountered that undefined index error
Jun 18 '09 #8

Dormilich
Expert Mod 5K+
P: 6,604
unchecked boxes (and radiobuttons) do not appear in $_POST.

do
Expand|Select|Wrap|Line Numbers
  1. var_dump($_POST);
to see what's present in the posted data.

you can also use isset() to check, if the variable/element exists.
Jun 18 '09 #9

crisis123
P: 18
thanks a lot
i didnt know about the fact nd wasnt getting any info regarding it
Jun 18 '09 #10

Dormilich
Expert Mod 5K+
P: 6,604
you may also experiment with the filter functions like filter_has_var().
Jun 18 '09 #11

crisis123
P: 18
can i use $_POST for upload section?

i didnt get any error on using it but is there a better way?
Jun 18 '09 #12

Dormilich
Expert Mod 5K+
P: 6,604
@crisis123
do you mean file upload? no, you need to use $_FILES for that.
Jun 18 '09 #13

crisis123
P: 18
thanks a lot
i have just started working on php
this is my 1st assignment
Jun 18 '09 #14

Dormilich
Expert Mod 5K+
P: 6,604

crisis123
P: 18
<input name="resume" type="file" />
$consultant_resume=$_FILES['resume'];

i am getting undefined index error for resume

can anyone help me define a variable for uploading a file
Jun 18 '09 #16

Dormilich
Expert Mod 5K+
P: 6,604
did you set the correct enctype in the form (multipart/form-data)? see file upload

again, check with
Expand|Select|Wrap|Line Numbers
  1. var_dump($_FILES);
Jun 18 '09 #17

crisis123
P: 18
$_POST worked instead of $_FILES
i am shocked
anyways i completed my assignment
thanku
its showing the link of the uploaded file in the txt file
nd thats what i wanted
Jun 18 '09 #18

Dormilich
Expert Mod 5K+
P: 6,604
@crisis123
then I guess you had the default data encryption type in your form…
Jun 18 '09 #19

crisis123
P: 18
yea may be
i still have a lot to learn
i am still a student of engineering
and this assignment was a part of my training
i used php for the 1st time today:)
Jun 18 '09 #20

Dormilich
Expert Mod 5K+
P: 6,604
gratulations for your first successful assignment. you're welcome to post back if you have any more questions.
Jun 18 '09 #21

crisis123
P: 18
if i have to create a webpage with a table containing all info stored in the form i created.
should i use file sys to store the record or using database is better?
Jun 22 '09 #22

Dormilich
Expert Mod 5K+
P: 6,604
that's up to your liking. although a database is often considered more secure, because it is not accessible like files from outside.
Jun 22 '09 #23

crisis123
P: 18
i am using files
i have to store each line of my txt file
in form of the table
wat code should i use??
Jun 22 '09 #24

Dormilich
Expert Mod 5K+
P: 6,604
check out PHP's filesystem functions.
Jun 22 '09 #25

32 Replies

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP php checkbox