469,300 Members | 2,057 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

multiple checkbox

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
35 76315
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
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
4,258 Expert 4TB
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
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
8,651 Expert Mod 8TB
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
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
sorry i have used $checkbox=$_POST['yes[]'] nd still encountered that undefined index error
Jun 18 '09 #8
Dormilich
8,651 Expert Mod 8TB
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
thanks a lot
i didnt know about the fact nd wasnt getting any info regarding it
Jun 18 '09 #10
Dormilich
8,651 Expert Mod 8TB
you may also experiment with the filter functions like filter_has_var().
Jun 18 '09 #11
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
8,651 Expert Mod 8TB
@crisis123
do you mean file upload? no, you need to use $_FILES for that.
Jun 18 '09 #13
thanks a lot
i have just started working on php
this is my 1st assignment
Jun 18 '09 #14
Dormilich
8,651 Expert Mod 8TB
a very valuable resource
Jun 18 '09 #15
<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
8,651 Expert Mod 8TB
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
$_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
8,651 Expert Mod 8TB
@crisis123
then I guess you had the default data encryption type in your form…
Jun 18 '09 #19
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
8,651 Expert Mod 8TB
gratulations for your first successful assignment. you're welcome to post back if you have any more questions.
Jun 18 '09 #21
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
8,651 Expert Mod 8TB
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
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
8,651 Expert Mod 8TB
check out PHP's filesystem functions.
Jun 22 '09 #25
i need to load the txt file line by line dynamically in the html table
will fgets() help me in this??
Jun 22 '09 #26
Dormilich
8,651 Expert Mod 8TB
if it does what it should, then yes. just read around in the manual (and the examples and comments), this should make it clear.
Jun 22 '09 #27
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??
Jun 23 '09 #28
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??
Jun 23 '09 #29
@crisis123

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??
Jun 23 '09 #30
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??
Jun 24 '09 #31
monsto
1
Hello,

Could somebody direct me to right direction. I want to make order form from checkboxes and radio buttons, I made working code for it, but I dont know how to make a calculation script for price. Because checkboxes or radio buttons has one value ( <input type="checkbox" name="papildoma[]" value="Order 1">, <input type="checkbox" name="papildoma[]" value="Order 2">), but how to point for these values price what could be calculated in total price?
Mar 30 '10 #32
1 <input type="checkbox" name="checkbox1" value="1" />
2 <input type="checkbox" name="checkbox" value="2" />
Apr 26 '12 #33
multiple select with checkbox

Expand|Select|Wrap|Line Numbers
  1. <form action="checkbox.php" method="post">
  2.   <input type="checkbox" name="checkbox" value="a">
  3.   <input type="checkbox" name="checkbox" value="b">
  4.   <input type="checkbox" name="checkbox" value="c">
  5.   <input type="checkbox" name="checkbox" value="d">
  6.   <br>
  7.   <br>
  8.   <input type="submit" name="Submit" value="Submit">
  9. </form>
checkbox.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $submit = $_POST['submit'];
  3. $checkbox = $_POST['checkbox'];
  4. if(isset($submit)){
  5. $check = count($checkbox);
  6. for($x=0;$x<$check;$x++){
  7. echo"$checkbox[$x]<br/>";
  8. }
  9. }
  10. ?>
  11.  
out put :
a
b
c
d
May 18 '13 #34
Dormilich
8,651 Expert Mod 8TB
looks correct, so far.
May 18 '13 #35
noyon
1
checkbok is database connect
May 19 '13 #36

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Ralph Freshour | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.