473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

PHP checkbox input

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)
Oct 23 '07 #1
6 12164
gregerly
192 Expert 100+
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
Oct 23 '07 #2
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
Oct 24 '07 #3
Atli
5,058 Expert 4TB
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.  
Oct 24 '07 #4
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?
Oct 24 '07 #5
Atli
5,058 Expert 4TB
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.
Oct 24 '07 #6
Thanks that help me out a lot!! : )

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.  
Jul 11 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jay | last post by:
Hi everybody ! I am currently writing a webpage that displays a list of records ( each record has a checkbox associated with it) to allow users to select any record they want to delete (much...
2
by: Fred | last post by:
Hi, I defined a form consisting of checkboxes like: <form> <input type="checkbox" name=ck id=ck onclick="check(this.form)" <input type="checkbox" name=ck id=ck onclick="check(this.form)" ........
4
by: Fabri | last post by:
How can I, on button click, to select ONLY the following 4 checkbox? I would like to do this without a for loop through form.lenght because this is only an example and I have to apply this script...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
5
by: _andrea.l | last post by:
I have n checkboxes and 1 checkbox 'SELECT ALL'. for example: <form action="" method="get"> <input name="sa" type="checkbox" value="v"> select all <input name="c1" type="checkbox" value="v">...
5
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
34
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript...
6
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.