Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    435
    Member #
    8983
    I want to be able to see what check boxes are checked using php what would be the best way to go about doing this:
    the boxes look like this:

    <input type="checkbox" name="in1" />
    <input type="checkbox" name="in2" />
    <input type="checkbox" name="in3" />
    <input type="checkbox" name="in4" />

    what I want to do is use them to delete records out of a database based on the number eg

    in1 = the id of 1 in the database
    what would be the best way of constructing a loop to find out the ones that are checked?
    Thee Pyro Wolf

  2.  

  3. #2
    Senior Member imagn's Avatar
    Join Date
    Jul 2007
    Location
    Los Angeles
    Posts
    156
    Member #
    15594
    Change the fields to:

    <input type="checkbox" name="db_ids[]" value="1" />
    <input type="checkbox" name="db_ids[]" value="2" />
    <input type="checkbox" name="db_ids[]" value="3" />
    <input type="checkbox" name="db_ids[]" value="4" />

    Then run this PHP code:

    foreach($_POST['db_ids'] AS $db_id) {
    $query = " DELETE FROM {table} WHERE {field} = $db_id ";
    $result = mysql_query($query);
    }

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    PHP has a strange way of dealing with checkboxes.

    If a box is unchecked, the variable is null.
    If the box is checked, the variable exists.

    This creates an interesting problem because an
    unchecked box essentially doesn't exist.

    Here's an example where the form has a checkbox named "Active"...

    $setActive= $_POST['Active'];
    if (isset($setActive)) {
    $setActive=1;
    } else {
    $setActive=0;
    }

    Or, you can do it this way ....

    $setActive= $_POST['Active'];
    if (isset($setActive)) {
    $setActive="on";
    } else {
    $setActive="off";
    }

    Now, there will always be a defined value for the checkbox, whether it's checked or not.


  5. #4
    Senior Member imagn's Avatar
    Join Date
    Jul 2007
    Location
    Los Angeles
    Posts
    156
    Member #
    15594
    Just add a check during the loop...

    foreach($_POST['db_ids'] AS $db_id) {
    if ($db_id) {
    $query = " DELETE FROM {table} WHERE {field} = $db_id ";
    $result = mysql_query($query);
    }
    }

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Actually, as it turns out, this is not PHP-specific. This is an issue with how browsers handle checkboxes; namely, only sending them when the checkbox is checked. There is a way around it, however, that I first saw in Rails. If you create a hidden field with the same name as the checkbox, with the default value you want (I believe it also has to be specified before the checkbox field), then you get that value when the checkbox is unchecked. e.g.:

    HTML Code:
    <input type="hidden" name="shouldDoMagic" value="no" />
    <label for="shouldDoMagic">Should Do Magic?</label><input type="checkbox" name="shouldDoMagic" id="shouldDoMagic" value="yes" />

  7. #6
    Senior Member
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    435
    Member #
    8983
    thanks alot it helped alot, oh and that work around also works when i tested it in ie
    Thee Pyro Wolf


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:34 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com