943,519 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1978
  • PHP RSS
Aug 16th, 2009
0

How to process checkboxes in php ?

Expand Post »
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 :
PHP Syntax (Toggle Plain Text)
  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 :

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Aug 16th, 2009
0

Re: How to process checkboxes in php ?

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:
PHP Syntax (Toggle Plain Text)
  1. <?
  2. $cr=$_REQUEST["cr"];
  3. $bt=$_REQUEST["bt"];
  4. $cr=="checkbox" if clicked or $cr=="" if not clicked
Reputation Points: 56
Solved Threads: 29
Posting Whiz in Training
sDJh is offline Offline
255 posts
since Aug 2005
Aug 16th, 2009
0

Re: How to process checkboxes in php ?

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
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Aug 16th, 2009
0

Re: How to process checkboxes in php ?

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:
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 56
Solved Threads: 29
Posting Whiz in Training
sDJh is offline Offline
255 posts
since Aug 2005
Aug 16th, 2009
0

Re: How to process checkboxes in php ?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009
Aug 17th, 2009
0

Re: How to process checkboxes in php ?

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.

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Aug 17th, 2009
0

Re: How to process checkboxes in php ?

Have you tried:
php Syntax (Toggle Plain Text)
  1. if($checkbox1=="helpivebeenclicked"){echo "User released the princess!";}
  2. else {echo "User denied clicking the frog.";}
?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apease11 is offline Offline
19 posts
since Feb 2009
Aug 17th, 2009
0

Re: How to process checkboxes in php ?

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 :
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help with drop down list and keyword search.
Next Thread in PHP Forum Timeline: mp3 upload from PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC