www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problems with Check-Boxes

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    4

    Problems with Check-Boxes

    Ugh! I'm not sure where to begin on what seems to be an insurmountable problem?!

    Okay, let me try and describe the issue...

    I built a Private Message feature into my website, and it looks very similar to Yahoo Mail.

    Everything works great, except for one last piece...

    Next to each PM in my "Inbox", there is a check-box. The idea is that you "check" certain Messages, and then when you select an "Action" from a drop-down in the Header and click "Go", my script applies the "action" to every message you checked. (Pretty standard, right?!)

    Now what I want to do, is make it so if a user checks the "Top Check-box" in the Header and clicks "Go", my script will apply the "action" (e.g. Mark as Unread") to every Message in my Inbox.

    What makes this tricky is that I do NOT want to use JavaScript. (I know it would be easier, but I have my reasons, so please respect that.)

    For the part of my script that does work - which is the ability to "check" certain Messages and have them updated - I am using an array in my Form and then passing it back to an UPDATE query.

    This works fine.

    But here is where I am dreadfully stuck...

    1.) I found out that if there are 20 Messages, and I just "check" 5 of them, then my array is only passing the 5 (and not all 20)?!

    2.) I tried creating another array, and populating it with the results directly from my SELECT query which populates the Inbox. The logic being, if I have 20 Messages in my Inbox, and populate this 2nd array with the 20 Message ID's, and then pass it back to my script via the $_POST array, then I would be able to run an UPDATE on all 20 Messages.

    But I can't get this part working...

    I am willing to post my code here, but maybe you guys could respond and let me know if what I have said so far makes sense, and if you think you can help...

    Sincerely,


    Debbie

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    One trick I've used is to create a type="hidden" <input> field to go along with each checkbox (since, as you've discovered, checkboxes only get posted if they are checked). I believe you want the checkboxes to come after their respective hidden fields, so that they overwrite the hidden fields if they are checked -- but I haven't done it in awhile, so you may have to experiment a bit ).

    Another alternative would be simply to save the relevant data in the session data, probably as an array within the $_SESSION array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    4
    My script has the following array which was populated by a query...

    Code:
    $testArray:
    
    array
      1 => int 52
      2 => int 51
      3 => int 49
      4 => int 39
      5 => int 38
      6 => int 10
      7 => int 8
      8 => int 6
      9 => int 5
      10 => int 2
      11 => int 1

    This script also has a Form, and when the user submits the Form, I want to pass the array above back to my script so it can be used on another query.


    In order to do this, I tried to pass the $testArray via the $_POST array like this...
    PHP Code:
                                                        <!-- Column Headings -->
        <
    tr>
            <
    th class='colSelect' />
                <
    input name='testArray[]' type='checkbox' value='TRUE' /> 

    At the top of my, is the following code to handle the Form being submitted...
    PHP Code:
        if ($_POST['pmAction']=="In_Unread"){
            echo 
    var_dump($_POST['testArray']);
            exit(); 

    However, when the Form is submitted, all I get is this output on my screen...

    Code:
    array
      0 => string 'TRUE' (length=4)

    Why isn't my $testArray being returned successfully in the $_POST array with all 11 values like described above?

    Sincerely,


    Debbie

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    See if this helps any:
    PHP Code:
    <!DOCTYPE html>
    <?php

    $testArray 
    = array(
      
    => 52,
      
    => 51,
      
    => 49,
      
    => 39,
      
    => 38,
      
    => 10,
      
    => 8,
      
    => 6,
      
    => 5,
      
    10 => 2,
      
    11 => 1
    );

    ?>
    <html>
    <head>
        <title>test</title>
        <style type='text/css'>
            form p {
                margin: 0;
            }
        </style>
    </head>
    <body>
    <h1>checkbox example</h1>
    <form action='' method='post'>
    <?php
    foreach($testArray as $test) {
        
    $checked '';
        if(!empty(
    $_POST['check'][$test]) and $_POST['check'][$test] == 'TRUE') {
            
    $checked " checked='checked'";
        }
        echo 
    "<p>
    <input type='hidden' name='check[
    $test]' value='FALSE' />
    <label><input name='check[
    $test]' type='checkbox' value='TRUE'$checked />$test</label>
    </p>
    "
    ;
    }
    ?>
    <p><input name='submit' type='submit' value='Submit' /></p>
    </form>
    <?php
    if(isset($_POST['submit'])) {
        echo 
    "<pre>".var_export($_POST['check'], true)."<pre>";
    }
    ?>
    </body>
    </html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles