SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php/mysql and html checkboxes

    I working on site for my school and i need to know a few things about checkboxes. I want each checkboxes to insert a true or false data into the feild of the sql database (or a 0/1 or anything else with 2 values). First, i need to know what type of feild i should make those sql feilds (text, int etc). and how i would get those values into the databases. i know how to get text in a textbox submitted, but how would it be different for checkboxes. Thanx!

  2. #2
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Checkboxes are either checked or not submitted. When you check to see what value was submitted, you need to check only if it is set, i.e. isset().

    As to the database, the best would be an enum field, since it will either be on or off. You can set it to be true/false, t/f, 1/0, etc.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx, that was really helpful. but one more thing, what would the html code look like? name="true"? and what is the () all about? i just started php about a month ago, so im not really that knowledgable about it.

  4. #4
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isset() is the function. You can go to php.net and click documentation to learn more about that.

    The name of the checkbox is the array key it will come through as ($_POST['name']). If it is checked, it will have the value given in the value="true" of the html code. If not checked, nothing of the checkbox goes through.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so it would like like <html name=info() value=true>? i know about the post['info'] thing, the thing im a little confused about is the html form code.. but thanx.

  6. #6
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not quite:

    <input type="checkbox" name="mybox" value="true">

    If it is checked, then $_POST['mybox'] would be set, with a value of "true". If not checked, then $_POST['mybox'] would not exist.

    So you would check like this:

    PHP Code:
    if ( isset( $_POST['mybox'] ) ) {

       
    $variable "true";

    }

    else {

       
    $variable "false";


    And you are ready to store the value in the database.

    Or however you want to do it.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx, that was really helpful, i got it working.now all i gotta do is get that info to come up on a dropdown menu, ut thats for a nother topic. Thanx again!


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
  •