mysql/php and checkboxes

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post June 21st, 2004, 9:58 am

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(">");}
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 21st, 2004, 9:58 am

  • Carnix
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 28, 2004
  • Posts: 1099
  • Status: Offline

Post June 21st, 2004, 11:24 am

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

.c
  • Alfie
  • Born
  • Born
  • No Avatar
  • Joined: Mar 09, 2006
  • Posts: 1
  • Status: Offline

Post March 9th, 2006, 8:11 am

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
  • Joined: Apr 28, 2004
  • Posts: 1099
  • Status: Offline

Post March 9th, 2006, 9:08 am

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
  • No Avatar
  • Joined: Sep 22, 2012
  • Posts: 1
  • Status: Offline

Post September 22nd, 2012, 10:07 pm

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 198 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.