Connecting Tech Pros Worldwide Help | Site Map

multiple checkbox

Newbie
 
Join Date: Oct 2006
Posts: 7
#1: Dec 11 '06
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
bishwadeep's Avatar
Newbie
 
Join Date: Nov 2006
Location: Nepal
Posts: 12
#2: Dec 11 '06

re: multiple checkbox


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>
Newbie
 
Join Date: Oct 2006
Posts: 7
#3: Dec 11 '06

re: multiple checkbox


Quote:

Originally Posted by bishwadeep

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
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Dec 11 '06

re: multiple checkbox


Quote:

Originally Posted by mudgilgaurav

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:
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#5: Jun 18 '09

re: multiple checkbox


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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Jun 18 '09

re: multiple checkbox


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…)
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#7: Jun 18 '09

re: multiple checkbox


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?
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#8: Jun 18 '09

re: multiple checkbox


sorry i have used $checkbox=$_POST['yes[]'] nd still encountered that undefined index error
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#9: Jun 18 '09

re: multiple checkbox


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.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#10: Jun 18 '09

re: multiple checkbox


thanks a lot
i didnt know about the fact nd wasnt getting any info regarding it
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#11: Jun 18 '09

re: multiple checkbox


you may also experiment with the filter functions like filter_has_var().
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#12: Jun 18 '09

re: multiple checkbox


can i use $_POST for upload section?

i didnt get any error on using it but is there a better way?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#13: Jun 18 '09

re: multiple checkbox


Quote:

Originally Posted by crisis123 View Post

can i use $_POST for upload section?

do you mean file upload? no, you need to use $_FILES for that.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#14: Jun 18 '09

re: multiple checkbox


thanks a lot
i have just started working on php
this is my 1st assignment
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#15: Jun 18 '09

re: multiple checkbox


a very valuable resource
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#16: Jun 18 '09

re: multiple checkbox


<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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#17: Jun 18 '09

re: multiple checkbox


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);
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#18: Jun 18 '09

re: multiple checkbox


$_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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#19: Jun 18 '09

re: multiple checkbox


Quote:

Originally Posted by crisis123 View Post

$_POST worked instead of $_FILES

then I guess you had the default data encryption type in your form…
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#20: Jun 18 '09

re: multiple checkbox


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:)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#21: Jun 18 '09

re: multiple checkbox


gratulations for your first successful assignment. you're welcome to post back if you have any more questions.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#22: Jun 22 '09

re: multiple checkbox


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?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#23: Jun 22 '09

re: multiple checkbox


that's up to your liking. although a database is often considered more secure, because it is not accessible like files from outside.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#24: Jun 22 '09

re: multiple checkbox


i am using files
i have to store each line of my txt file
in form of the table
wat code should i use??
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#25: Jun 22 '09

re: multiple checkbox


check out PHP's filesystem functions.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#26: Jun 22 '09

re: multiple checkbox


i need to load the txt file line by line dynamically in the html table
will fgets() help me in this??
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#27: Jun 22 '09

re: multiple checkbox


if it does what it should, then yes. just read around in the manual (and the examples and comments), this should make it clear.
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#28: Jun 23 '09

re: multiple checkbox


my txt file stores records in the following format
johnny| AR| manager| awesome| regional| 90| 1| 2342009| C:\\Documents and Settings\\abc\\Desktop\\Thanks.doc
this is one line of the file nd the information stored when the form is filled once
what i want is to display this as a table.......that is johhny should be in one coloumn Ar in another..........and so on
fgets command is storing the entire line in a single coloumn
any suggestions??
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#29: Jun 23 '09

re: multiple checkbox


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Mainpage</title></head>
  3. <body>
  4. <table border = "1">
  5. <th>Consultant Name</th>
  6. <th>Consultant Location</th>
  7. <th>Consultant Job Title</th>
  8. <th>Consultant SKILLS</th>
  9. <th>Consultant Summary</th>
  10. <th>Consultant Rate</th>
  11. <th>Consultant Relocate</th>
  12. <th>Consultant Last Assignment Date</th>
  13. <th>Consultant Resume</th>
  14. <?php
  15.  
  16. $file = fopen("users.txt", "r") or exit("Unable to open file!");
  17. for($i=0; $i<=10; $i++)
  18. {
  19.     echo "<tr>";
  20.     for($j=0; $j<=9; $j++)
  21.     {
  22.         $test = fgets($file);;
  23.         echo "<td>";
  24.         echo "$test";
  25.         echo "</td>";
  26.     }
  27.         echo "</tr>";
  28. }
  29. fclose($file);
  30.  
  31. ?>
  32. </table>
  33. </body>
  34. </html>

this is how i am storing txt file in html table
but if i leave any coloumn after last assignment date
the next set of records dont begin from the next row

any changes in this code??
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#30: Jun 23 '09

re: multiple checkbox


Quote:

Originally Posted by crisis123 View Post

<html>
<head><title>Mainpage</title></head>
<body>
<table border = "1">
<th>Consultant Name</th>
<th>Consultant Location</th>
<th>Consultant Job Title</th>
<th>Consultant SKILLS</th>
<th>Consultant Summary</th>
<th>Consultant Rate</th>
<th>Consultant Relocate</th>
<th>Consultant Last Assignment Date</th>
<th>Consultant Resume</th>
<?php

$file = fopen("users.txt", "r") or exit("Unable to open file!");
for($i=0; $i<=10; $i++)
{
echo "<tr>";
for($j=0; $j<=9; $j++)
{
$test = fgets($file);;
echo "<td>";
echo "$test";
echo "</td>";
}
echo "</tr>";
}
fclose($file);

?>
</table>
</body>
</html>


this is how i am storing txt file in html table
but if i leave any coloumn after last assignment date
the next set of records dont begin from the next row

any changes in this code??
Newbie
 
Join Date: Jun 2009
Location: Delhi
Posts: 18
#31: Jun 24 '09

re: multiple checkbox


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Mainpage</title></head>
  3. <body>
  4. <table border = "1">
  5. <th>S.No.</th>
  6. <th>Select row</th>
  7. <th>Consultant Name</th>
  8. <th>Consultant Location</th>
  9. <th>Consultant Job Title</th>
  10. <th>Consultant SKILLS</th>
  11. <th>Consultant Summary</th>
  12. <th>Consultant Rate</th>
  13. <th>Consultant Relocate</th>
  14. <th>Consultant Last Assignment Date</th>
  15. <th>Consultant Resume</th>
  16. <?php
  17. $file = fopen("users.txt", "r") or exit("Unable to open file!");
  18. for($i=1; $i<=10; $i++)
  19. {
  20. ?>
  21. <tr><td><?echo $i?></td>
  22. <td>
  23. <input type="radio" name="select" value="<?echo $i;?>"/>
  24. <?
  25.     echo "</td>";
  26.     for($j=1; $j<=9; $j++)
  27.     {
  28.         $test = fgets($file);
  29.         echo "<td>";
  30.         echo "$test";
  31.         echo "</td>";
  32.     }
  33.         echo "</tr>";
  34. }
  35. fclose($file);
  36. ?>
  37. </table>
  38. <a href="form.php"><font size="2">Add</font></a>
  39. <form action="mainpage.php" name="form"<?echo $i?> method="post">
  40. <input type="hidden" name="variablename" value=".$i."/>
  41. <input type="submit" value="Delete"/>
  42. </form>
  43. </body>
  44. </html>
this is the code i have used for creating a html table
however on clicking the delete button the selected row is not deleted
any suggestions??
Reply