How to process checkboxes in php ?

Reply

Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

How to process checkboxes in php ?

 
0
  #1
Aug 16th, 2009
hello everyone ! ,
I am creating a form with various check boxes in it. The check boxes represent various type of workshop the person wants to attend.

i just dont know how to process the data in check boxes.

check.html
The html code is :
  1. <html>
  2. <head>
  3. <title>Untitled Document</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. </head>
  6.  
  7. <body>
  8. <p>Please select the check boxes to for registering for the workshop</p>
  9. <form name="form1" method="post" action="check.php">
  10. <input type="checkbox" name="cr" value="checkbox">
  11. Clinical REsearch
  12. </form>
  13. <form name="form2" method="post" action="check.php">
  14. <input type="checkbox" name="bt" value="checkbox">
  15. Biotechnology
  16. </form>
  17. <form name="form3" method="post" action="check.php">
  18. <input type="checkbox" name="mc" value="checkbox">
  19. Medical stuff
  20. </form>
  21. <form name="form4" method="post" action="check.php">
  22. <input type="checkbox" name="none" value="checkbox">
  23. None of them , i was kidding...... heheheheh
  24. </form>
  25. <form name="form5" method="post" action="check.php">
  26. <input type="submit" name="Submit" value="Register me now">
  27. </form>
  28. <p>&nbsp; </p>
  29. </body>
  30. </html>

the check.php code is :

  1. <?php
  2.  
  3. $dbc = mysqli_connect('localhost','root','','aliendb')
  4. or die('error connecting to MySql');
  5.  
  6. $fin = 'hey';
  7.  
  8. if( (isset($_POST['cr'])) && (isset($_POST['bt'])) && (isset($_POST['mc'])) )
  9. {
  10. $fin = "clinical research" . '</br>' . "Biotech" . '</br>' . "Medical crap";
  11. }
  12.  
  13. echo 'ur registered no go home ';
  14.  
  15.  
  16. $query = "INSERT INTO checkbox(workshop)".
  17. "VALUES ('$fin')";
  18.  
  19. $result = mysqli_query($dbc , $query) or die('Error querying db');
  20.  
  21. mysqli_close($dbc);
  22.  
  23. ?>


wat i want is when the person clicks on one or more of the boxes , in my database the corresponding data must be entered in workshop coloumn.

example:

if the person clicks on
1.Medical Biotechnolgy
2.CLinical REsearch

Then in the workshop coloumn , these two names must be entered by the query.

I would really appreciate if u guys can help me out.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 150
Reputation: sDJh is an unknown quantity at this point 
Solved Threads: 13
sDJh sDJh is offline Offline
Junior Poster

Re: How to process checkboxes in php ?

 
0
  #2
Aug 16th, 2009
You just need a single form-element, which goes around all of your checkboxes and a submit-button at the very end. Then the state of the checkboxes are represented in PHP by simply having a variable named after your checkbox set to the text you put in the "value"-element. If the user hasn't clicked the box, the variable keeps empty.
Example:
  1. <?
  2. $cr=$_REQUEST["cr"];
  3. $bt=$_REQUEST["bt"];
  4. $cr=="checkbox" if clicked or $cr=="" if not clicked
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: How to process checkboxes in php ?

 
0
  #3
Aug 16th, 2009
SDjh , well i am not quite getting in wat r u trying to tell.
can u quote a simple example on how a single check box is processed , may be i am lacking that part,
so i guess u don't use $_POST in case of check boxes rather u use
$_Request.

Well sorry if this sounds ridiculous , i am a tyro in PhP
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 150
Reputation: sDJh is an unknown quantity at this point 
Solved Threads: 13
sDJh sDJh is offline Offline
Junior Poster

Re: How to process checkboxes in php ?

 
0
  #4
Aug 16th, 2009
The difference between $_POST and $_REQUEST is simply, that $REQUEST handles POST as well as GET variables. So don't mind about this.

Now seeing if the user has selected a checkbox you can simply access the variable in PHP. Example:
  1. Your form:
  2. <form method=post action="output.php">
  3. <label><input type=checkbox name="checkbox1" value="helpivebeenclicked">Please click me, I look like a frog, but I'm a princess!</label>
  4. <input type=submit>
  5. </form>
  6.  
  7. Your PHP-Skript "output.php"
  8. <?
  9. $checkbox1=$_REQUEST["checkbox1"];
  10. if($checkbox1=="helpivebeenclicked"){echo "User released the princess!";}
  11. if($checkbox1==""){echo "User denied clicking the frog.";}
  12. ?>
  13.  

If you now name the checkbox "whatevericanthinkof" the variable in PHP is also called "whatevericanthinkof". I you set the value of the checkbox to "donthaveanyidearightnow" the variable will be set to "danthaveanyidearightnow" and so on.
If the user hasn't clicked the box, the variable is simply an empty string.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 17
Reputation: apease11 is an unknown quantity at this point 
Solved Threads: 0
apease11 apease11 is offline Offline
Newbie Poster

Re: How to process checkboxes in php ?

 
0
  #5
Aug 16th, 2009
Just do this, $checkbox1=$_POST["checkbox1"]; (like sDJh said) and then preform:

if ($checkbox1) { ... Your Code ... }

That worked fine for me. It just checks to see if the check box has been changed at all from it's default value of null. I can email you my working script also if you would like.
Last edited by apease11; Aug 16th, 2009 at 6:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: How to process checkboxes in php ?

 
0
  #6
Aug 17th, 2009
sDjH .. kudos to you.
But the script is working properly when the check box is checked
but if its not , its giving me a parse error.

  1. Notice: Undefined index: checkbox1 in C:\wamp\www\db\output.php on line 2
  2. User denied clicking the frog.

i wrote the output.html and output.php separately
When the check box is clicked its fine , when its not its displaying the message as well as the parse error . Is there ne way to remove the error with some text or so for that condition that would be great.
Last edited by rahul8590; Aug 17th, 2009 at 12:59 pm. Reason: spelling mistake
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 17
Reputation: apease11 is an unknown quantity at this point 
Solved Threads: 0
apease11 apease11 is offline Offline
Newbie Poster

Re: How to process checkboxes in php ?

 
0
  #7
Aug 17th, 2009
Have you tried:
  1. if($checkbox1=="helpivebeenclicked"){echo "User released the princess!";}
  2. else {echo "User denied clicking the frog.";}
?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: How to process checkboxes in php ?

 
0
  #8
Aug 17th, 2009
sorry , i didnt mention this , but i tried that method also when i was getting parse error.
Well basically the parse error is in line 2
which is :
  1. $checkbox1=$_REQUEST["checkbox1"];

this Undefined index error as i said before , is generated only on not clicking the check box , but works fine when clicked.
Last edited by rahul8590; Aug 17th, 2009 at 1:11 pm.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC