SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot manju75's Avatar
    Join Date
    Jul 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to insert Checkbox value to database

    I need one checkbox data to send to mysql database using php

    my database is "mydatabase"
    table name "datatb"
    column name "show"

    when ckeckbox is ckecked it should insert "yes" into column "show"
    when ckeckbox is unckecked it should insert "no" into column "show"

    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are asking for the php code do do this? Your post says you "need one checkbox data "?
    Some of my domains:
    www.r8s.us/allsites.htm

  3. #3
    SitePoint Zealot manju75's Avatar
    Join Date
    Jul 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, I want to use only one checkbox.

  4. #4
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ...and what is the problem you are having with it?

    Here is the general idea:

    PHP Code:
    $query "INSERT INTO datatb(show) VALUES('";

    if (isset(
    $_POST['checkbox_name']) && ($_POST['checkbox_name'] == "value")) {
     
    $query .= "yes";
    } else {
     
    $query .= "no";
    }

    $query .= "');";

    mysql_query($query); 
    Obviously, you include whatever other fields you have in the table, and perform appropriate checking / escaping where relevant. It also assumes you have opened a DB connection already.

  5. #5
    SitePoint Zealot manju75's Avatar
    Join Date
    Jul 2006
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    according to your script checkbox should be like this?


    <input type="checkbox" name="Ckekboxname" value="yes" >

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The values for the checkbox are context sensitive, depending on how you intend to use them.

    Tip: If the checkbox is not checked, it's not even included in the submitted form.

    The check for ($_POST['checkbox_name'] == "value") means that you should replace the name and value with whatever makes sense from your page.

    Code HTML4Strict:
    <input type="checkbox" name="receive_pamphlets" value="yes">

    Code PHP:
    if (isset($_POST['receive_pamphlets']) && ($_POST['receive_pamphlets'] == "yes")) {
     $query .= "yes";
    } else {
     $query .= "no";
    }
    Last edited by paul_wilkins; Dec 12, 2007 at 04:55.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    An alternate method that uses HTML rather than an if statement in PHP to test if the field exists.

    HTML Code:
    <input type="hidden" name="receive_pamphlets" value="no">
    <input type="checkbox" name="receive_pamphlets" value="yes">
    With the HTML coded that way 'yes' will be passed if the checkbox is checked and 'no' will be passed if it is not.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    using the over-ridden hidden form field is a very nice way to ensure that some value gets sent.

    Should he perform the test in PHP anyway, as a part of validation to make sure someone hasn't tried to get a bad value in there?

  9. #9
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually use integers for yes no values and translate them if needed on output for display. In that case, you could still use the hidden field, but if the value wasn't present it would still register as 0 or no when it was type cast ($choice = (int) $_POST['checkboxname']). The hidden field method is especially nice if you have a lot of checkboxes and need to keep track of the numerical order regardless of which one is checked and which one isn't.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pmw57 View Post
    using the over-ridden hidden form field is a very nice way to ensure that some value gets sent.

    Should he perform the test in PHP anyway, as a part of validation to make sure someone hasn't tried to get a bad value in there?
    You should always validate everything from the form on the server to ensure that the values passed are valid and have not been tampered with.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    A value for the checkbox doesn't need to be included, but it can provide useful context if you're seeing the submitted results somewhere else.
    Actually, value is a required attribute for checkboxes.

    Quote Originally Posted by felgall View Post
    An alternate method that uses HTML rather than an if statement in PHP to test if the field exists.

    HTML Code:
    <input type="hidden" name="receive_pamphlets" value="no">
    <input type="checkbox" name="receive_pamphlets" value="yes">
    With the HTML coded that way 'yes' will be passed if the checkbox is checked and 'no' will be passed if it is not.
    But you cannot have two elements with the same name in valid HTML, surely?

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Actually, value is a required attribute for checkboxes.
    You're right, I should've checked the spec before coming up with that. It's a good thing I've used a value on them as a matter of cause.

    Quote Originally Posted by Stormrider View Post
    But you cannot have two elements with the same name in valid HTML, surely?
    Yes you can, just think about how radio buttons work.

    Checking the spec shows the following

    Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.

  13. #13
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah, ok. So how does it work when you have multiple values selected? How to they get returned in $_POST? As an array?

  14. #14
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the example of the hidden field, the value that is transmitted is the last non blank value in source code order. Therefore if the checkbox is not checked what comes through is the value of the hidden field. If the checkbox is checked, then it is last in source code order and will have it's value sent.

    You can however use array syntax with any kind of form field.

    HTML Code:
    <input type="checkbox" name="chkname[]" value="1" />
    
    <select name="date[0]">
       <option value="1">January</option>
    </select>
    
    <input type="text" name="modelnumber[]" />
    Any of those will work, and bring back an array to PHP. In fact if you want to use a multiple select list, you have to use array syntax for the name, in order for PHP to receive all the selected items.

    Keep in mind, that Javascript doesn't look at the array syntax in the name, only PHP. To javascript the name attribute is a literal string, regardless. Items named the same thing are part of a named node map, but naming a form control..

    HTML Code:
    <input type="checkbox" name="favoritefood[0]" value="chicken" />
    means nothing to Javascript. You would still address it like..

    Code:
    document.forms['myform'].elements['favoritefood[0]'];

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cool, cheers

    I knew about the array syntax, but not about having multiple names the same without the array syntax.

  16. #16
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always pass a numeric value in the checkbox field as Hammer65 has shown:

    HTML Code:
    <input type="checkbox" name="active" value="1" />
    Then since checkboxes don't pass any value (they are not set) if they aren't checked, it's easy to test for on the backside with isset():
    PHP Code:
    $active = isset($_POST['active']) ? 0
    The you save the NUMERIC value into your database in a TINYINT(1) field for MySQL (for other databases, you can use a proper BOOLEAN column type). Any other time you need to test the value, you can just put the variable from your database into an 'if' statement, and if becomes very easy since 0 evaluates as false:
    PHP Code:
    if($record->active) {
      
    // do stuff

    Numeric 1/0 values are easier to store and understand across languages, easier to test for, and require less disk space to store than text. Always use numeric 1/0 values for simple on/off stored data.

  17. #17
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Cool, cheers

    I knew about the array syntax, but not about having multiple names the same without the array syntax.

    A common error some newbies make in their forms is to give several fields the same name without using an array and then wonder why only the last of those fields actually gets passed. The use of a hidden field with the same name as a checkbox is one of the few instances where giving two fields the same name is meaningful - the only other one I can think of is radio buttons. In each case only one value will be passed with that value decided by which radio button is selected or whether the checkbox is or isn't checked. The only other time it would work for multiple fields having the same name without using an array is if you use JavaScript to control which of them are actually in the form.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  18. #18
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think it would be more efficient to process the checkbox on the other side, rather than have to send out an extra hidden field every time, but ah well


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •