PHP checkbox input | Member | | Join Date: Sep 2007
Posts: 61
| | |
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)
|  | Expert | | Join Date: Sep 2006
Posts: 189
| | | 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
| | | 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: -
<input type=radio name='finance' value='true'/>Yes
-
<input type=radio checked name='finance' value='false'/>No
-
but I want to use this second way: -
<input name="auto" type="checkbox" id="interest1" value="Automotive" /> Automotive
-
and the value couldn't be send. can anybody tell me how to use the second way successfully?
thnx
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,754
| | | 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: -
<form action="?" method="POST">
-
<input type="checkbox" name="check" value="true" />Check me
-
<br /><input type="submit" name="submitButton" />
-
</form>
-
<?php
-
if(isset($_POST['submitButton'])) {
-
if(isset($_POST['check'])) {
-
echo "Checkbox was checked!";
-
}
-
else {
-
echo "Checkbox was NOT checked";
-
}
-
}
-
else {
-
echo "Form has not been submitted!";
-
}
-
?>
-
| | Member | | Join Date: Sep 2007
Posts: 61
| | | 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?
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,754
| | | 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: -
$v1 = "TRUE";
-
$v2 = false;
-
$v3 = 1;
-
-
$QUERY = "INSERT INTO tbl(BoolField) VALUES($v1), ($v2), ($v3)";
-
-
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
| | | 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: -
<form action="?" method="POST">
-
<input type="checkbox" name="check" value="true" />Check me
-
<br /><input type="submit" name="submitButton" />
-
</form>
-
<?php
-
if(isset($_POST['submitButton'])) {
-
if(isset($_POST['check'])) {
-
echo "Checkbox was checked!";
-
}
-
else {
-
echo "Checkbox was NOT checked";
-
}
-
}
-
else {
-
echo "Form has not been submitted!";
-
}
-
?>
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|