SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update Multiple Records EVEN IF UNCHECKED

    Hello,
    I have a table which displays a list of locations.
    One of the columns in that table is called "showonwebsite".
    If the db contains a "y" for a specific record, it checks the box.

    I am trying to do it so when the website admin checks the box, for multiple records in the table and presses submit, it adds the 'y' to those records.
    So far this is working.

    The problem is when they "uncheck" a record, I cant find a way to make it remove the 'y' from the database.

    Here is what I have.

    Form:
    HTML Code:
    <form action="managelocations.php" method="post">
    <input <?php if (!(strcmp($row_rsActiveLocations['showinfooter'],"y"))) {echo "checked=\"checked\"";} ?> name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row_rsActiveLocations['locationsid']; ?>">
    <input name="submit" type="submit" value="submit"/>
    </form>
    PHP
    PHP Code:
    <?php
    if(isset($_POST['checkbox'])){$update $_POST['checkbox'];
    mysql_select_db($database_db$db);
    foreach (
    $update as $value) {
    $option='y';
    $query "UPDATE locations SET showonwebsite='$option' WHERE locationsid = $value"
    $Result mysql_query($query$db) or die(mysql_error());


    }
    ?>
    I have tried toying with if thens and all types of variations but no luck.
    Anyones help is truly appreciated!

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The problem is you cannot loop over checkboxes in $_POST because checkboxes are submitted only if they are checked. If a checkbox is not checked then it is not sent to the server at all so in php you have no way of detecting its existence. In other words, you can only loop over checked checkboxes. There are two solutions how to handle this problem:

    1. If the number of checkboxes and their names are predefined and constant then you need to define all of them in php and check the state of each one with isset($_POST[$checkbox_name]).

    2. If you have a list of checkboxes and you don't know in advance how many of them there are you can add a hidden field in your form to accompany each checkbox so then you loop over the hidden fields, which are always submitted. The hidden fields only mark the existence of a checkbox and are identified by having the same key as the checkbox. For example:
    PHP Code:
    <!-- repeat this code for each location, we assume that locationsid is unique for each location: -->
    <input <?php if (!(strcmp($row_rsActiveLocations['showinfooter'],"y"))) {echo "checked=\"checked\"";} ?> name="checkbox[<?php echo $row_rsActiveLocations['locationsid']; ?>]" type="checkbox">
    <input type="hidden" name="checkbox_mark[<?php echo $row_rsActiveLocations['locationsid']; ?>]" value="1">
    And you gather the information like this:
    PHP Code:
    if(isset($_POST['checkbox_mark'])){
      
    mysql_select_db($database_db$db);
      
      
    // the value (locationsid) is passed in hidden input's key, therefore
      // the value of the input itself is irrelevant ($dummy)
      
    foreach ($_POST['checkbox_mark'] as $value => $dummy) {
        
    $option = isset($_POST['checkbox'][$value]) ? 'y' 'n';
        
    $query "UPDATE locations SET showonwebsite='$option' WHERE locationsid = $value"
        
    $Result mysql_query($query$db) or die(mysql_error());
      } 

    BTW, you cannot have id="checkbox[]" in multiple inputs on the same page since id must be unique.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked like a charm! THANKS!
    Would you be kind enough to explain this code?
    PHP Code:
    $option = isset($_POST['checkbox'][$value]) ? 'y' 'n'

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It's shorthand notation for a simple if/else statement and is equivalent to:
    PHP Code:
    if (isset($_POST['checkbox'][$value])) {
      
    $option 'y';
    } else {
      
    $option 'n';

    it means: if the checkbox is checked, then set $option to 'y', otherwise to 'n'. $value is the key part of the checkbox name in square brackets, not the actual <input> value. The <input> value is irrelevant here because we only need to know if it was sent or not - so it doesn't have to even be defined in HTML (if you don't define checkbox's value then most browsers send the string "on" as the value, therefore in this example $dummy will contain "on").


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
  •