954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to process checkboxes in php ?

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 :

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Please select the check boxes to for registering for the workshop</p>
<form name="form1" method="post" action="check.php">
  <input type="checkbox" name="cr" value="checkbox">
  Clinical REsearch 
</form>
<form name="form2" method="post" action="check.php">
  <input type="checkbox" name="bt" value="checkbox">
  Biotechnology 
</form>
<form name="form3" method="post" action="check.php">
  <input type="checkbox" name="mc" value="checkbox">
  Medical stuff 
</form>
<form name="form4" method="post" action="check.php">
  <input type="checkbox" name="none" value="checkbox">
  None of them , i was kidding...... heheheheh 
</form>
<form name="form5" method="post" action="check.php">
  <input type="submit" name="Submit" value="Register me now">
</form>
<p>&nbsp; </p>
</body>
</html>


the check.php code is :

<?php

$dbc = mysqli_connect('localhost','root','','aliendb')
       or die('error connecting to MySql');

$fin = 'hey';

if( (isset($_POST['cr'])) && (isset($_POST['bt'])) && (isset($_POST['mc']))  )
{
	$fin = "clinical research" . '</br>' . "Biotech" . '</br>' . "Medical crap";
}

echo 'ur registered no go home ';


$query = "INSERT INTO checkbox(workshop)".
          "VALUES ('$fin')";

$result = mysqli_query($dbc , $query) or die('Error querying db');

mysqli_close($dbc);

?>

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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

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:

<?
$cr=$_REQUEST["cr"];
$bt=$_REQUEST["bt"];
$cr=="checkbox" if clicked or $cr=="" if not clicked
sDJh
Posting Whiz in Training
259 posts since Aug 2005
Reputation Points: 56
Solved Threads: 29
 

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

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

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:

Your form:
<form method=post action="output.php">
<label><input type=checkbox name="checkbox1" value="helpivebeenclicked">Please click me, I look like a frog, but I'm a princess!</label>
<input type=submit>
</form>

Your PHP-Skript "output.php"
<?
$checkbox1=$_REQUEST["checkbox1"];
if($checkbox1=="helpivebeenclicked"){echo "User released the princess!";}
if($checkbox1==""){echo "User denied clicking the frog.";}
?>


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.

sDJh
Posting Whiz in Training
259 posts since Aug 2005
Reputation Points: 56
Solved Threads: 29
 

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.

apease11
Newbie Poster
19 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Notice: Undefined index: checkbox1 in C:\wamp\www\db\output.php on line 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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

Have you tried:

if($checkbox1=="helpivebeenclicked"){echo "User released the princess!";}
else {echo "User denied clicking the frog.";}

?

apease11
Newbie Poster
19 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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 :

$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.

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You