mysql/php and checkboxes

  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

how do i code the mysql to take into consideration the checkboxes that will be on my html form? what variables to i need to use in the code. i have got some code for the php part of it. please check to see that it it right.

Code: [ Select ]
<td align="left" valign="middle"><?php echo "<input type='checkbox' name='samples' value='yes' class='biggertext'/> ";?></td>


the checkbox is basically a 'tick for yes' and 'no tick for no'

in this code below i havent named all the fields for the insert because of the checkboxes. is that right?

Code: [ Select ]
$sql = "insert into personell(name, company, email) values ('{$_POST['name']}','{$_POST['company']}','{$_POST['email']}')";
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

buzzby365 wrote:
how do i code the mysql to take into consideration the checkboxes that will be on my html form? what variables to i need to use in the code. i have got some code for the php part of it. please check to see that it it right.

Code: [ Select ]
<td align="left" valign="middle"><?php echo "<input type='checkbox' name='samples' value='yes' class='biggertext'/> ";?></td>


the checkbox is basically a 'tick for yes' and 'no tick for no'

in this code below i havent named all the fields for the insert because of the checkboxes. is that right?

Code: [ Select ]
$sql = "insert into personell(name, company, email) values ('{$_POST['name']}','{$_POST['company']}','{$_POST['email']}')";


yes or no what? what are they confirming or rejecting?
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

If you are only asking one question, and what a checkable item for yes and no, use radio buttons instead. If you want a single box (check for yes, uncheck for no), then a single checkbox will do.

The simplist way to explain it, without writting the code for you is:

create a field in mySQL, call it whatever you want (I'll call it "samples" here). Make it a TinyInt with a length of 1 and set it to have a default value of 0. (Note, in case you are not familiar. 1 = YES or TRUE, 0 = No or FALSE. It's a standard convention in most programming languages).

For a checkbox, where check is for yes, unchecked is for no, use the following instead:
Code: [ Select ]
<input type='checkbox' name='samples' value='1' class=''biggertext'/>


Now, on whatever PHP page is processing this request, grab the POST collection (assuming your form is using the POST method):

Code: [ Select ]
$samples = $_POST["samples"];


Then, simply UPDATE or INSERT (whatever floats your boat... :wink: ) that value to the database. The EXACT same idea would work for a radio button, since you name them the same (samples) and therefore only one value is passed. Just put 0 for NO and 1 for YES as the values...

Does this makes sense?

.c
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

i am unsure as to what to do. i have a form with checkboxes. i would like php to handle this. how do i code the php and mysql to handle this. i am cool with php/mysql for other elements including dropdown stuff. just not sure with the ckeckbox or radio button. which is best. the checkbox will be used as a yes/no item. would radio button be better for this? how does this relate to php coding and mysql database alotting?
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Just name the checkboxes different things:
Code: [ Select ]
<input type="checkbox" name="box1" VALUE="1" />
<input type="checkbox" name="box2" VALUE="1" />
<input type="checkbox" name="box3" VALUE="1" />

<input type="radio" name="confirm " VALUE="0" CHECKED/> NO
<input type="radio" name="confirm " VALUE="1" /> YES
  1. <input type="checkbox" name="box1" VALUE="1" />
  2. <input type="checkbox" name="box2" VALUE="1" />
  3. <input type="checkbox" name="box3" VALUE="1" />
  4. <input type="radio" name="confirm " VALUE="0" CHECKED/> NO
  5. <input type="radio" name="confirm " VALUE="1" /> YES


Then, in your PHP"

PHP Code: [ Select ]
 
$box1 = $_POST["box1"];
 
$box2 = $_POST["box2"];
 
$box3 = $_POST["box3"];
 
$confirm = $_POST["confirm "];
 
 
  1.  
  2. $box1 = $_POST["box1"];
  3.  
  4. $box2 = $_POST["box2"];
  5.  
  6. $box3 = $_POST["box3"];
  7.  
  8. $confirm = $_POST["confirm "];
  9.  
  10.  



If you're using GET, instead, you'd use $_GET. I usually use a foreach statement to itterate through the $_POST and $_GET collections instead of using explicit calls like this, but it doesn't really matter.

in mySQL, you would create 4 fields, one for each checkbox and on for the radio, unless you'd going to do something complicated with a SET or ENUM field type by combining the checkbox options, that is. I find it easier to just have a bunch of boolean fields than writing a long script that can intrepret SET or ENUM data... but that's just me.

.c
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

so if i want all checkboxes to remain unchecked until the user checks those boxes my "box1" will have a value="0"

right?
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

No, it will still have VALUE="1"

Checkboxes are unchecked by default in HTML. If you want it to be check automatically, then you add "checked" to the HTML tag as a parameter:

[code]<input type="checkbox" name="box1" VALUE="1" CHECKED />/code]

The value is what gets submited when the box is checked. If the box isn't check, that element is not submitted (or the value is empty... either way, a value is only passed if the box is checked).

If you want to have previously checked boxes be checked when a user comes back, you can do it one of two ways. You can use JavaScript to check boxes by dynamically writting some JS with PHP, or, more simply, just have PHP write CHECKED in the checkbox tag if the value in the mySQL database is 1, otherwise don't.

A (very) simple way to do this would be (not, this may not be the best way, it's only for example):

PHP Code: [ Select ]
 
... mySQL query here ...
 
 
 
$db_box1 =  ...data from the box1 field ...
 
 
 
if($db_box1 == 1) { $set_checked = " CHECKED";}
 
else{$set_checked = ""; }
 
 
 
print "<input TYPE=\"checkbox\" NAME="box1" VALUE=\"1\" . $set_checked . "/>";
 
 
 
 
  1.  
  2. ... mySQL query here ...
  3.  
  4.  
  5.  
  6. $db_box1 =  ...data from the box1 field ...
  7.  
  8.  
  9.  
  10. if($db_box1 == 1) { $set_checked = " CHECKED";}
  11.  
  12. else{$set_checked = ""; }
  13.  
  14.  
  15.  
  16. print "<input TYPE=\"checkbox\" NAME="box1" VALUE=\"1\" . $set_checked . "/>";
  17.  
  18.  
  19.  
  20.  


This is, obviously, an ultra simplifed example, but hopefully it helps.
.c
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

ok. i wont do anything to the form because '1' is only passed if the form is checked. and by '1' the value would be 'true' or 'yes' or something positive. if the checkbox isnt filled in then whatever does go thru (if anything) would be to the opposite or nothing at all which is still the opposite to 'some value' or '1' or something. i understand now.

cheers
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

buzzby365 wrote:
ok. i wont do anything to the form because '1' is only passed if the form is checked. and by '1' the value would be 'true' or 'yes' or something positive. if the checkbox isnt filled in then whatever does go thru (if anything) would be to the opposite or nothing at all which is still the opposite to 'some value' or '1' or something. i understand now.

cheers


The way I usually check for the existance of a variable is

PHP Code: [ Select ]
 
$var = (empty($_POST['checkboxName'])) ? -1 : $_POST['checkboxName']
 
 
  1.  
  2. $var = (empty($_POST['checkboxName'])) ? -1 : $_POST['checkboxName']
  3.  
  4.  


This sets the empty variable to a value of -1. I don't know what most guys do but I try to develop my PHP apps with REGISTER GLOBALS off thus the need for the check.
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

Code: [ Select ]
<input type="checkbox" name="box1" VALUE="1" />
<input type="checkbox" name="box2" VALUE="1" />
<input type="checkbox" name="box3" VALUE="1" />

<input type="radio" name="confirm " VALUE="0" CHECKED/> NO
<input type="radio" name="confirm " VALUE="1" /> YES
  1. <input type="checkbox" name="box1" VALUE="1" />
  2. <input type="checkbox" name="box2" VALUE="1" />
  3. <input type="checkbox" name="box3" VALUE="1" />
  4. <input type="radio" name="confirm " VALUE="0" CHECKED/> NO
  5. <input type="radio" name="confirm " VALUE="1" /> YES


i am a little confused about the values, can the value be 'yes' or does the value have to be '1' ? is this a boolean type thing? confused as to what readout i will get in the database. if i ask a question giving 4 answers, the user can choose to select 1 or all 4. when i read the input submitted i want to see 'yes' next to the questions asked. if i put '1' in the value, do i take that to mean true or yes?
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

re reading the above post by carnix has helped me to see that a value of 1 needs to be put in. when a value is returned i will see if it is 1 or 0 and then decipher whether it is true or false.
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

Carnix wrote:
Just name the checkboxes different things:
Code: [ Select ]
<input type="checkbox" name="box1" VALUE="1" />
<input type="checkbox" name="box2" VALUE="1" />
<input type="checkbox" name="box3" VALUE="1" />

<input type="radio" name="confirm " VALUE="0" CHECKED/> NO
<input type="radio" name="confirm " VALUE="1" /> YES
  1. <input type="checkbox" name="box1" VALUE="1" />
  2. <input type="checkbox" name="box2" VALUE="1" />
  3. <input type="checkbox" name="box3" VALUE="1" />
  4. <input type="radio" name="confirm " VALUE="0" CHECKED/> NO
  5. <input type="radio" name="confirm " VALUE="1" /> YES


Then, in your PHP"

PHP Code: [ Select ]
$box1 = $_POST["box1"];
$box2 = $_POST["box2"];
$box3 = $_POST["box3"];
$confirm = $_POST["confirm "];
 
  1. $box1 = $_POST["box1"];
  2. $box2 = $_POST["box2"];
  3. $box3 = $_POST["box3"];
  4. $confirm = $_POST["confirm "];
  5.  




what is confusing to me is why have you got the radio button listed 2wice. one for yes and one for no. is this necessary? i have many radio buttons on my site where questions with more than 2 answers are possible but 1 is required. do i need to list the button 2wice for every radio button?
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

looking thru my php4dummies book (i am a total novice) i see that the value doesnt have to be '1' but can actually be the value i want. ie. i dont have to have this set up
Code: [ Select ]
<input type="checkbox" name="project_garden" VALUE="1" />


instead i can have this set up:
Code: [ Select ]
<input type="checkbox" name="project" VALUE="garden" />


what is the difference between these 2 statements? is there an advantage to using one over the other?

i dont want to use any other complicated set ups for this form. no SET or ENUM stuff. i just want to track what projects my clients want undertaken. another form will be for how far a particular project has gone and so on.
  • buzzby365
  • Proficient
  • Proficient
  • buzzby365
  • Posts: 288

Post 3+ Months Ago

Code: [ Select ]
aged    Tinyint(1) default '0',
is the way to set a default field of zero in mysql? its a checkbox and therefore if it has been omitted then the value is '0' otherwise it is the value of the checkbox whether it be '1' or 'garden'. is that right?
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

On the checkbox questions:
Yes, you're right you can use any value you want. However, I find it much easier to use a boolean expression than to go a string comparison. 1 is intrepreted as boolean TRUE and 0 is intrepreted as boolean FALSE. If you use other numbers, or other strings, you have to test specifically for that number or string. It's up to you, I just find it easier since you can write code like if($var){} instead of if($var=="blah"){} when you use straight boolean. You're choice. The way I would do it is just print the text you want if TRUE:

PHP Code: [ Select ]
 
if($var){ print "yes";}
 
else{ print "no";}
 
 
  1.  
  2. if($var){ print "yes";}
  3.  
  4. else{ print "no";}
  5.  
  6.  



On radio buttons:
Radio buttons a bit special. If, by default, none are selected, then obviously the value is empty. But once one is selected, you cannot unselect it (only toggle between different options of the same name), and therefore a value will ALWAYS be returned (unless the VALUE attribute of one is blank of course...).

If you only have one radio option, if a user selects that option by accident, they can't unselect it without refreshing the page, which is obvioulsy not a good thing. Radio buttons are designed for the "Select only one of the following" type scenerios. Checkboxes are "Select all that apply" sort of things. If you only have one option, radio buttons are not appropriate (unless you use onclick javascript methods to unselect the radio button... but I think that's pretty silly personally, checkboxes are much easier).

On the default value question:

I don't recall off hand what they exact SQL syntax is to set it is, but in theory you're correct. You can set a default value, so that when a new record is created in the database, it will have that value, unless another value is assigned to it by the INSERT (in other words, you don't have to bother including that value unless it's something other than the default value). I usually use 0 (FALSE) as my defaults, but in some cases 1 would be appropriate. If you're using something other than the boolean method, I would suggest not assigning a default value and letting the field be NULL by default. Then, you can check for NULL or NOT NULL in the SQL to return items with a value... then check for the value you need (SELECT field WHERE box1=1; seems easier to me, but it's your application =])

Hope that helps!
.c
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

not sure if this is what your looken for but this will set the box to checked or not depending on whats in the database

PHP Code: [ Select ]
<input type=checkbox name=var value=1");
 
 
 
if($var == 1) {
 
echo(" CHECKED>");}
 
else {
 
echo(">");}
  1. <input type=checkbox name=var value=1");
  2.  
  3.  
  4.  
  5. if($var == 1) {
  6.  
  7. echo(" CHECKED>");}
  8.  
  9. else {
  10.  
  11. echo(">");}
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Not to be picky but.. *points up in the thread where this code was already posted*

.c
  • Alfie
  • Born
  • Born
  • Alfie
  • Posts: 1

Post 3+ Months Ago

hi i know this topic is quite old but i think you guys may be able to help me out?

i have been creating a form and it submits all the correct details to a mysql database which is fine, however i have also put a checkbox in.. when the checkbox is selected all is ok and the value "1" is submitted, however the checkboxe returns nothing (neither a name nor a value) if unchecked and sends the user an error saying : Notice: Undefined index: none in http:\\location\ on line 26

i know why its doing it but i cant figure out how to solve it..

does it need an if unchecked do something else type value?

PHP Code: [ Select ]
$none = $_POST....
 
 
 
...... <input type="checkbox" name="none" value="1">
  1. $none = $_POST....
  2.  
  3.  
  4.  
  5. ...... <input type="checkbox" name="none" value="1">


Thanks in advance

Alfie
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Define your PHP variable first, maybe equal to 0. If the checkbox value exists in the HTTP collection, then set the value equal to it. If it doesn't, then your value will be 0. Checkbox type form values are only passed if the box is checked, they don't get passed at all otherwise.

.c
  • DoNTblink1882
  • Born
  • Born
  • DoNTblink1882
  • Posts: 1

Post 3+ Months Ago

Skimming through this I didn't see anything that really answered the question so I'll give it a shot. Keep in mind I'm a programmer and a motto around here is "friends don't let friends use javascript." I'm not a fan of interpreted languages such as PHP because its so loosely coded but on the web you don't have much of a choice so my solution would be to create a function for example:

PHP Code: [ Select ]
<form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<input type="checkbox" name="box1" VALUE="1" />
</form>
 
<?php
 
$checkbox = $_POST['box1'];
function isChecked($check_box_name) {
     if (!empty($check_box_name)) {
          return true;
     } else {
          return false;
     }
}
 
if (isChecked($checkbox)) {
     echo 'Checked';
} else {
     echo 'Not checked';
}
?>
 
  1. <form action="<?php $_SERVER['SCRIPT_NAME']; ?>" method="POST">
  2. <input type="checkbox" name="box1" VALUE="1" />
  3. </form>
  4.  
  5. <?php
  6.  
  7. $checkbox = $_POST['box1'];
  8. function isChecked($check_box_name) {
  9.      if (!empty($check_box_name)) {
  10.           return true;
  11.      } else {
  12.           return false;
  13.      }
  14. }
  15.  
  16. if (isChecked($checkbox)) {
  17.      echo 'Checked';
  18. } else {
  19.      echo 'Not checked';
  20. }
  21. ?>
  22.  


One reason I don't like interpreted languages is because you cant define a variable's data type. I don't know how PHP returns booleans(my guess is false = 0, true = 1) or even how it would recognize one so I'd much rather create a function to act in a way that I expect. I haven't tested this code but I don't see why it wouldn't work.

If you want further explanation on the code, the way I understand check boxes in html is they only return a value if they're checked. So if we use an if statement to check if the value is not empty it must be checked therefore it will return true otherwise return false and for one checkbox the parameter for the function isChecked() is unnecessary, I included the parameter because you may have multiple check boxes so you can use the same function for all check boxes by storing the data returned by each check box in the form in its own variable and inserting that variable you want to check so isChecked($checkbox) tells the function to return the value for $checkbox. Also instead of returning true or false you can return any value you'd like of any data type (if there's a way to define data types in this sloppy language). If you have multiple check boxes you can define what to do if different combinations of them are selected by expanding the if statement in the function.

Finally, just because I don't like this particular language don't underestimate it, PHP is a very powerful language that can do a lot of different things and my opinion doesn't change that.

Post Information

  • Total Posts in this topic: 20 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.