PHP Checkbox Help

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, I'm new to PHP; however, I'm picking up really quickly. I need help with the action script of a form that I'm developing. I'm developing a form that is nothing more than a check box to select when something is finished. So, the items that need to be finished are static and I already have the form complete.

What I'm wanting to do is with my form open, I select that I have finished 3 things and click the checkbox that the item is finished. When I click the checkbox and click save, it post a 1 in the database for that field and all the other checkbox that aren't check are set to NULL. Now, the next time I open that form up, I can see all the items that have been checked and the ones that haven't been checked, based on if there is a 1 or a 0 in the database for that item.

Any help would be greatly appreciated!!!

Thank you,
Jordon Shaw
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I assume that you already know how to set the database up and how to connect it and how to retrieve the information. I would go from here to tell/inform you on how you could accomplish this thing.

What I'm using here is a ternary operator. Basically a one line if/else statement that is mostly used to define variables.

Here is the ternary operator and an if/else side by side in descriptive terms.

((if) ? then : else);

(if)
{
then
}
else
{
else
}

The 'if' part is the validation you are doing (such as is_null($variable) )

The bold words in both examples go along in the same place (function-wise). I think you understand what this is. Hopefully :)

Code: [ Select ]
<?php
/*
  * Checking which of the boxes were checked.
  * If the checkbox is  null, than it's empty, if it's not not, then it was checked.
  * Makes the variable '0' if not checked and '1' if checked.
  * Using the ternary operator to do this.
  *
  * Assuming that the checkboxes are named checkbox1, checkbox2, ...
  */
$checkbox1 = ((is_null($_POST['checkbox1'])) ? 0 : 1);
$checkbox2 = ((is_null($_POST['checkbox2'])) ? 0 : 1);
$checkbox3 = ((is_null($_POST['checkbox3'])) ? 0 : 1);
 
// The SQL to update the table in the database named 'table_name'
$sql = "UPDATE table_name SET checkbox1 = {$checkbox1}, checkbox2 = {$checkbox2}, checkbox3 = {$checkbox3}";
// The mysql query that carries out the sql and setting the variable to true if it's successfull
$process = mysql_query($sql) or die(mysql_error());
// Checking if the query was made successfully.
if($process)
{
    // The query was successfull. Print the following text in the browser.
    echo "table_name was updated successfully";
}
?>
  1. <?php
  2. /*
  3.   * Checking which of the boxes were checked.
  4.   * If the checkbox is  null, than it's empty, if it's not not, then it was checked.
  5.   * Makes the variable '0' if not checked and '1' if checked.
  6.   * Using the ternary operator to do this.
  7.   *
  8.   * Assuming that the checkboxes are named checkbox1, checkbox2, ...
  9.   */
  10. $checkbox1 = ((is_null($_POST['checkbox1'])) ? 0 : 1);
  11. $checkbox2 = ((is_null($_POST['checkbox2'])) ? 0 : 1);
  12. $checkbox3 = ((is_null($_POST['checkbox3'])) ? 0 : 1);
  13.  
  14. // The SQL to update the table in the database named 'table_name'
  15. $sql = "UPDATE table_name SET checkbox1 = {$checkbox1}, checkbox2 = {$checkbox2}, checkbox3 = {$checkbox3}";
  16. // The mysql query that carries out the sql and setting the variable to true if it's successfull
  17. $process = mysql_query($sql) or die(mysql_error());
  18. // Checking if the query was made successfully.
  19. if($process)
  20. {
  21.     // The query was successfull. Print the following text in the browser.
  22.     echo "table_name was updated successfully";
  23. }
  24. ?>

I commented the code telling what it does.

I'm pretty sure that this would work, although I haven't tested it. Obviously you need to be connected to the sql database you are using and the database selected before you can use the mysql_query(); function.

Good luck
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Thank you very much for your help! I will try that and see if it works and let you know!!! I do know how to do the connection to SQL and everything like that. Still learning, but it's going great so far. I love php!!! :D
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

This code did work. Whenever I select a check box and hit save, it does post a 1 in the field if it's check, it also post a 0 if it's not check. So that's working perfect!

The second part of this is whenever I go to the page, I want it to query the DB and if there is a 1 then it should show it checked. This peice isn't working with this code that you have given me.

I'm sure that I will sure that I will use a If then statement with a query, but I'm not exactly positive how to do that. If you can help me with that, that would be awesome!

Thanks,
Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Alright. From here, it would be easy. Here is the commented code so you would know what it's doing.
PHP Code: [ Select ]
<?php
// Example values
$row = array();
$row['checkbox1'] = 1;
$row['checkbox2'] = 0;
$row['checkbox3'] = 1;
 
// Checking if checkbox has a 1... if so, put the HTML attribute to checked="checked" otherwise make the variable null (empty)
$checkbox1 = (($row['checkbox1'] == 1) ? ' checked="checked"' : null);
$checkbox2 = (($row['checkbox2'] == 1) ? ' checked="checked"' : null);
$checkbox3 = (($row['checkbox3'] == 1) ? ' checked="checked"' : null);
 
// Setting the HTML with the variables in place
$html = <<<EOT
Checkbox 1: <input type="checkbox" name="checkbox1"$checkbox1 /><br />
Checkbox 2: <input type="checkbox" name="checkbox2"$checkbox2 /><br />
Checkbox 3: <input type="checkbox" name="checkbox3"$checkbox3 />
EOT;
 
// Echoing the HTML
echo $html;
?>
  1. <?php
  2. // Example values
  3. $row = array();
  4. $row['checkbox1'] = 1;
  5. $row['checkbox2'] = 0;
  6. $row['checkbox3'] = 1;
  7.  
  8. // Checking if checkbox has a 1... if so, put the HTML attribute to checked="checked" otherwise make the variable null (empty)
  9. $checkbox1 = (($row['checkbox1'] == 1) ? ' checked="checked"' : null);
  10. $checkbox2 = (($row['checkbox2'] == 1) ? ' checked="checked"' : null);
  11. $checkbox3 = (($row['checkbox3'] == 1) ? ' checked="checked"' : null);
  12.  
  13. // Setting the HTML with the variables in place
  14. $html = <<<EOT
  15. Checkbox 1: <input type="checkbox" name="checkbox1"$checkbox1 /><br />
  16. Checkbox 2: <input type="checkbox" name="checkbox2"$checkbox2 /><br />
  17. Checkbox 3: <input type="checkbox" name="checkbox3"$checkbox3 />
  18. EOT;
  19.  
  20. // Echoing the HTML
  21. echo $html;
  22. ?>

Hope that this helps and makes sense
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, maybe I'm not fully understanding. Let me show you my code and then if you can tell me where to put what in there to pull if the check box has a 1 to make it checked and then also allow me to check the box and post a 1 if I check it. This way you can see my varibles.

Code: [ Select ]
<?php
    $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
        if(!$connection)
        {
        die('Something went wrong while connecting to MSSQL');
        }
    $db_select = mssql_select_db(DB_NAME, $connection);
    if (!$db_select){
        die("Database selection failed");
    }
?>
<?php
$checkbox1 = ((is_null($_POST['checkbox1'])) ? 0 : 1);
$checkbox2 = ((is_null($_POST['checkbox2'])) ? 0 : 1);
$checkbox3 = ((is_null($_POST['checkbox3'])) ? 0 : 1);
 
 
$sql = "UPDATE checkbox SET checkbox1 = {$checkbox1}, checkbox2 = {$checkbox2}, checkbox3 = {$checkbox3}";
$process = mssql_query($sql) or die(mssql_error());
 
if($process)
{
    echo "Checkbox was updated successfully";
}
?>
  1. <?php
  2.     $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
  3.         if(!$connection)
  4.         {
  5.         die('Something went wrong while connecting to MSSQL');
  6.         }
  7.     $db_select = mssql_select_db(DB_NAME, $connection);
  8.     if (!$db_select){
  9.         die("Database selection failed");
  10.     }
  11. ?>
  12. <?php
  13. $checkbox1 = ((is_null($_POST['checkbox1'])) ? 0 : 1);
  14. $checkbox2 = ((is_null($_POST['checkbox2'])) ? 0 : 1);
  15. $checkbox3 = ((is_null($_POST['checkbox3'])) ? 0 : 1);
  16.  
  17.  
  18. $sql = "UPDATE checkbox SET checkbox1 = {$checkbox1}, checkbox2 = {$checkbox2}, checkbox3 = {$checkbox3}";
  19. $process = mssql_query($sql) or die(mssql_error());
  20.  
  21. if($process)
  22. {
  23.     echo "Checkbox was updated successfully";
  24. }
  25. ?>


Of course I defined the connections with DB_SERVER, DB_USER, DB_PASS, DB_NAME

So, what do you think?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

The code I gave you above is to show you what to do when you are RETRIEVING the information. Right now, you can just copy/paste that code into a PHP document, save it and run it and you will see three checkboxes with two that has a 1 are checked and the one that has a 0 is not checked...

Here is how I would do it using mysql. (Although you are using mssql, I never used that database so I can't tell you from there, although I assume the code would be nearly the same).

PHP Code: [ Select ]
<?php
mysql_connect($host, $user, $pass);
mysql_select_database($dbname);
 
$sql = 'SELECT * FROM checkbox';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
 
$checkbox1 = (($row['checkbox1'] == 1) ? ' checked="checked"' : null);
$checkbox2 = (($row['checkbox2'] == 1) ? ' checked="checked"' : null);
$checkbox3 = (($row['checkbox3'] == 1) ? ' checked="checked"' : null);
 
// Setting the HTML with the variables in place
$html = <<<EOT
Checkbox 1: <input type="checkbox" name="checkbox1"$checkbox1 /><br />
Checkbox 2: <input type="checkbox" name="checkbox2"$checkbox2 /><br />
Checkbox 3: <input type="checkbox" name="checkbox3"$checkbox3 />
EOT;
 
// Echoing the HTML
echo $html;
?>
  1. <?php
  2. mysql_connect($host, $user, $pass);
  3. mysql_select_database($dbname);
  4.  
  5. $sql = 'SELECT * FROM checkbox';
  6. $result = mysql_query($sql);
  7. $row = mysql_fetch_assoc($result);
  8.  
  9. $checkbox1 = (($row['checkbox1'] == 1) ? ' checked="checked"' : null);
  10. $checkbox2 = (($row['checkbox2'] == 1) ? ' checked="checked"' : null);
  11. $checkbox3 = (($row['checkbox3'] == 1) ? ' checked="checked"' : null);
  12.  
  13. // Setting the HTML with the variables in place
  14. $html = <<<EOT
  15. Checkbox 1: <input type="checkbox" name="checkbox1"$checkbox1 /><br />
  16. Checkbox 2: <input type="checkbox" name="checkbox2"$checkbox2 /><br />
  17. Checkbox 3: <input type="checkbox" name="checkbox3"$checkbox3 />
  18. EOT;
  19.  
  20. // Echoing the HTML
  21. echo $html;
  22. ?>

That's all to it. I don't know what you don't understand about it.

All that code does is add the attribute if checkbox1 is equal to '1' than add the attribute checked="checked" to the HTML, otherwise, don't add it.

This is to work along side with the first piece of code that I gave you to UPDATE the checkboxes... this is not a different set of code to replace it... this is to extend it.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

That worked perfectly! Thank you very much for your help!!!

Jordon
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, first I want to say that I greatly appreciate this help. Now, I would like to extend on this project a little. Basically what I'm creating is grid that will allow me to check a step off when I'm developing classes. It will look like this when it's done: http://www.shawhome.net/cms/test/grid.html

As you can see, there are many check boxes on here, so I was trying to do this with the least amount of code as possible. What I was thinking is to only post to the database a 1 if it's checked and not have anything in there if it's not check. If you uncheck it, then change the 1 to a 0, since the record will already be there.

So, I have two tables, Course and Checkbox. My course table has course_id and course_name. The check box table has ck_id, course_id, value_name, checked.

So, what I was thinking is that whenever you click on save, it will post the course_id, based on whatever course I have selected, it would then post the value_name, such as checkbox1 or checkbox50 and then if it's checked it would post a 1.

Whenever I reload that course again, then it will display all the checkboxes that have a 1 in the checkbox table.

Does this sound like a good method and if so, can you help with the code any?

Thanks,
Jordon Shaw
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Well, I was going to suggest to code it but it looks like you have done something... maybe you just changed the HTML but... whatever :lol:
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Actually, I've changed that link, it's not valid anymore. I don't want to provide a link on here, because a link would allow anybody to post to my database, now that I have the majority of it working.

I do appreciate your help!!!

Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Alright... good. Glad to hear that :D Your welcome.
  • Thunx1
  • Born
  • Born
  • Thunx1
  • Posts: 1

Post 3+ Months Ago

Hey i know this is very late and hope you still browse this but, what is the MySQL configuration you use on this, having a hard time figuring out how to set up the database, i mean how does the table look :P
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Row `checkbox` is int.

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 153 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
 
 

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