Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP checkbox input

Member
 
Join Date: Sep 2007
Posts: 61
#1: Oct 23 '07
I want to write a PHP register form which uses checkbox to gain user's interests, for example, sport/ travelling/ gaming/ reading etc. Making the html form is easy but can anybody tell me how to create a appropriate database for it, so that later I can print all those interests as user's profile or that sort of thing?

I created some db fields for some certain interests, but I didn't know which type to choose so that it can hold value from the post method (i'm using Xampp)

gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#2: Oct 23 '07

re: PHP checkbox input


Quote:

Originally Posted by tuananh87vn

I want to write a PHP register form which uses checkbox to gain user's interests, for example, sport/ travelling/ gaming/ reading etc. Making the html form is easy but can anybody tell me how to create a appropriate database for it, so that later I can print all those interests as user's profile or that sort of thing?

I created some db fields for some certain interests, but I didn't know which type to choose so that it can hold value from the post method (i'm using Xampp)

Hi Tuananh87vn,

You have alot of options. It all depends on how you want to set up your database. You could have a single table to hold all user info, which would hold things like first name, last name, and all their intrests with a yes or no field. (A good datatype for yes or no fields is enum, the enum datatype only allows you to enter certain chars. like 'y' for yes, or 'n' for no). You could also have a relational database where you would store the user info in one table, and the intrests in another. That is a little more complicated, however it's probably the right way to do it. let me know how you want to do it and I can elaborate.

Greg
Member
 
Join Date: Sep 2007
Posts: 61
#3: Oct 24 '07

re: PHP checkbox input


I think that enum is useful when we try dropdown menu or radio (2 options:yes & no) , but I tried it with checkbox type where there's only one box to check.

it worked when I tried this, with boolean type:
Expand|Select|Wrap|Line Numbers
  1. <input type=radio name='finance' value='true'/>Yes
  2. <input type=radio checked name='finance' value='false'/>No
  3.  
but I want to use this second way:
Expand|Select|Wrap|Line Numbers
  1. <input name="auto" type="checkbox" id="interest1" value="Automotive" /> Automotive
  2.  
and the value couldn't be send. can anybody tell me how to use the second way successfully?

thnx
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: Oct 24 '07

re: PHP checkbox input


Hi.

Personally, I would use the BOOL data type to store this sort of data, as it's only values are 1 or 0 (TRUE or FALSE), needing only one bit to be stored.

The checkbox input is not included in the POST if it is not checked. That is; if it is checked it's value will be sent, if not the <input> will be ignored completely.

So, if you want to check whether a checkbox has been checked, use the isset() function:
Expand|Select|Wrap|Line Numbers
  1. <form action="?" method="POST">
  2.   <input type="checkbox" name="check" value="true" />Check me
  3.   <br /><input type="submit" name="submitButton" />
  4. </form>
  5. <?php
  6. if(isset($_POST['submitButton'])) {
  7.   if(isset($_POST['check'])) {
  8.     echo "Checkbox was checked!";
  9.   }
  10.   else {
  11.     echo "Checkbox was NOT checked";
  12.   }
  13. }
  14. else {
  15.   echo "Form has not been submitted!";
  16. }
  17. ?>
  18.  
Member
 
Join Date: Sep 2007
Posts: 61
#5: Oct 24 '07

re: PHP checkbox input


so I'll setup a BOOL type for this value in the database, which holds only 1 or 0? but the value from the form says "true". we understand it's 1 but how comes it?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#6: Oct 24 '07

re: PHP checkbox input


In both PHP and MySQL, 'TRUE' is an alias for 1 (and everything higher), and 'FALSE' is an alias for 0.

So if you are inserting into a BOOL field in your database, you should be able to do this without any problems:
Expand|Select|Wrap|Line Numbers
  1. $v1 = "TRUE";
  2. $v2 = false;
  3. $v3 = 1;
  4.  
  5. $QUERY = "INSERT INTO tbl(BoolField) VALUES($v1), ($v2), ($v3)";
  6.  
  7.  
Note, that the the TRUE and FALSE keywords are case-insensitive.
Also, if you insert anything other than 0 into a BOOL field, it will be interpreted as TRUE.
Newbie
 
Join Date: Jul 2008
Posts: 1
#7: Jul 11 '08

re: PHP checkbox input


Thanks that help me out a lot!! : )

Quote:

Originally Posted by Atli

Hi.

Personally, I would use the BOOL data type to store this sort of data, as it's only values are 1 or 0 (TRUE or FALSE), needing only one bit to be stored.

The checkbox input is not included in the POST if it is not checked. That is; if it is checked it's value will be sent, if not the <input> will be ignored completely.

So, if you want to check whether a checkbox has been checked, use the isset() function:

Expand|Select|Wrap|Line Numbers
  1. <form action="?" method="POST">
  2.   <input type="checkbox" name="check" value="true" />Check me
  3.   <br /><input type="submit" name="submitButton" />
  4. </form>
  5. <?php
  6. if(isset($_POST['submitButton'])) {
  7.   if(isset($_POST['check'])) {
  8.     echo "Checkbox was checked!";
  9.   }
  10.   else {
  11.     echo "Checkbox was NOT checked";
  12.   }
  13. }
  14. else {
  15.   echo "Form has not been submitted!";
  16. }
  17. ?>
  18.  

Reply