SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing on Checkbox Value

    Here's the problem...

    I'm building another Image Management Module for a client. This one includes the feature of selecting which pages they would like the images to appear on. I do this on the upload form with checkboxes.

    Here's the thing... I'm inserting the value of the checkbox "1" or "" into my database.

    When I go to insert the checkbox value in my upload form I'm running the following:

    PHP Code:
    if($pagename === "1") {

    $insert "INSERT into table(field) VALUES ('$pagename')";
    $query mysql_query($insert);

    Each checkbox has a value of "1" when checked. I'm running a similar if() as above on each checkbox to determine where to INSERT into the database and where not. However, when the form is submitted, checked or unchecked, it's submitting a value of "1" into the database.

    I've checked the $_POST with an echo() statement and it's only posting the ones checked with a 1. The unchecked boxes are showing as empty strings. Should I be using an :

    if(IsSet($checkbox) {

    to test the checkbox?
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    Las Vegas
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You said each checkbox has a value of "1" when checked, and that you are finding "1"'s in your database. Isn't that expected?

    By the way your code snippet tells us little about your actual problem. What is $pagename? Try posting more of your PHP, as well as a couple of your HTML check boxes.

    UPDATE

    I'm sorry. I didn't read your post as carefully as I should have.

    When I'm working with check boxes, I use:

    PHP Code:
        if($_POST['nameOfCheckBox']) {
            echo 
    'Checked.';
        } 
    To test whether something was checked. No need for isset() or anything.

    When dealing with checkboxes, once your user has hit submit, all boxes checked exist, and those that weren't, don't.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rabbit
    To test whether something was checked. No need for isset() or anything.
    WRONG!!! that will produce notice if the checkbox is not checked..
    Quote Originally Posted by Rabbit
    When dealing with checkboxes, once your user has hit submit, all boxes checked exist, and those that weren't, don't.
    that's why you should use exactly the isset() function... that will not work if the value of checkbox is set to zero..
    Quote Originally Posted by Rabbit
    PHP Code:
        if($_POST['nameOfCheckBox']) {
            echo 
    'Checked.';
        } 
    but KDesigns doesn't use zeroes as values, so the best way to do this IMHO is with empty():
    PHP Code:
    $pagename = ((isset($_POST['pagename'])) ? ((int) $_POST['pagename']) : 0);
    if(!empty(
    $pagename)) { 
      
    $insert "INSERT INTO table (field) VALUES ('$pagename')"
      
    $query mysql_query($insert); 


  4. #4
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    Las Vegas
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All that works, too.

    (Good point about the incorrect determination in the event a "0" value was encountered.)

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    KDesigns,

    I'd suggest that you encode the page names into your checkbox values, something like this:
    HTML Code:
    <label><input type="checkbox" name="pages[]" value="page1" /> Page 1?</label>
    <label><input type="checkbox" name="pages[]" value="page2" /> Page 2?</label>
    <label><input type="checkbox" name="pages[]" value="page3" /> Page 3?</label>
    Then you can ask:

    PHP Code:
    foreach ($_POST['pages'] as $page_name)
    {
        
    add_image_to_page($image$page_name);


  6. #6
    SitePoint Member
    Join Date
    May 2004
    Location
    California
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does isset do?

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kiropetsnet
    what does isset do?
    isset() function checks if specified variable exists and returns TRUE if so, or FALSE if it doesn't.. check out PHP Manual for more info..


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
  •