Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Passing Array in $_POST

    I could really use some help trying to figure this problem/script out.

    Here is what I am trying to do...

    I built a Private Messaging module for my website, and everything works great except for one tiny thing.

    My PM system looks very similar to Yahoo Mail, with a Header Row across the top which has these columns: Select, Flag, From, Message, Date.

    In the Select Column, I have a check-box next to each Message. This allows a user to check certain Message, and then choose a value (e.g. "Mark as Read") in a drop-down list in the Header when they press "Go".

    I have the check-boxes working if a user cherry-picks individual messages, chooses an "action" and presses "Go".

    But what I also want, is the ability to check the Check-box in the Header, and when the user selects an "action" and "Go", my PHP applies the "action" to EVERY Message even though only the Top Check-box was checked. (Follow me?!)

    Now, a KEY CONSTRAINT here is that I do NOT want to use JavaScript. That simple. Period. (No arguments, please!!)

    Here is the server-side strategy I have come up with, and I think I'm close to getting it working, but I just need some help with passing an array in my $_POST array...

    At the top of my PHP script I have...
    PHP Code:
        // Initialize Test Array.
    $incomingArray = array(); 

    A little farther down I have...
    PHP Code:
        // ***************************************
        // HANDLE FORM.                         *
        // ***************************************
    // Form was Submitted (Post).

            // ****************************
            // Check for Message Action.    *
            // ****************************
    if ($_POST['pmAction']=="In_Unread"){

    // **************************
                // Mark Incoming as Unread.    *
                // **************************

    foreach($_POST['inMsgArray'] as $msgID => $msgValue){

    // This doesn't work, but I feel it should?!
    //            foreach($_POST['incomingArray'] as $msgNo => $inMsgArray['pmID']){

                    // Build query.
    $q1 "UPDATE private_msg_recipient
                            SET read_on=NULL,
                            WHERE member_id_to=?
                            AND message_id=?
                            LIMIT 1"

    // Prepare statement.
    $stmt1 mysqli_prepare($dbc$q1);

    // Bind variables to query.

    // This doesn't work, but I feel it should?!
    //                mysqli_stmt_bind_param($stmt1, 'ii', $sessMemberID, $inMsgArray['pmID']);

                    // Execute query.

    // Verify Update.
    if (mysqli_stmt_affected_rows($stmt1)==1){
    // Update Succeeded.
    $redirectView 'incoming';


    Then farther down in my PHP I have...
    PHP Code:
    // Form was not Submitted (Get).

            // **************************
            // Determine PM View-Type.    *
            // **************************
    if (isset($_GET['msgview']) && $_GET['msgview']){
    // Message-View found in URL.

                // More code here...

                    // ****************************
                    // Create 'Incoming' Dataset.    *
                    // ****************************
                    // Execute query.

    // Store results.

    // Check # of Records Returned.
    if (mysqli_stmt_num_rows($stmt6) > 0){
    // Messages Found.
    $messagesExist TRUE;

    // Bind result-set to variables.

                        // Set Incoming Array Counter.
    $i 1;

    // Fetch record.
    while (mysqli_stmt_fetch($stmt6)){
    // Build Incoming Array.
    $incomingArray[$i] = array('pmID' => $pmID,
    'readOn' => $readOn,
    'flag' => $flag,
    'fromID' => $fromID,
    'fromUsername' => $fromUsername,
    'subject' => $subject,
    'sentOn' => $sentOn);
    $i $i 1;


    Finally in HTML section I have...
    PHP Code:
        // ****************************
        // Create 'Incoming' Output.    *
        // ****************************

    <!-- LOOK HERE -->
    Column Headings -->
    th class='colSelect' />
    input name='incomingArray[]' type='checkbox' value='TRUE' />
    th id='colFlag'>
    img src='/images/Flag_Red_20x22.png' width='15' alt='' />
    th id='colFrom'>From</th>
    th id='colSubject'>Subject</th>
    th id='colDate'>Date</th>

    Body Data -->

        // Retrieve Messages from Array.
    $incomingArray as $msgNo => $inMsgArray){
            echo "
    <tr" . (is_null($inMsgArray['readOn']) ? " class='pmUnread'" : "") . ">
    td class='colSelect'>
    input id '" . str2htmlentities($inMsgArray['pmID']) . "' " .
    name inMsgArray[" . str2htmlentities($inMsgArray['pmID']) . "" .
    type 'checkbox' " .
    value 'TRUE'

    Problem Summarized:
    If I check individual messages and then choose an "action" (e.g. "Mark as Read"), and then "Go", my script works.

    The problem I am trying to solve, is a way to pass the $incomingArray - which contains a listing of all Incoming Messages displayed - and pass that array in my $_POST array, so that when my Form is submitted, and if I determine that the Top Check-box was checked, then I can grab the listing of all Incoming Messages from this array that I stuffed in the $_POST array, and use that when I run my UPDATE Query.

    As I found out the hard way, even though I might have 20 check-boxes corresponding to 20 Incoming Messsages, if you don't check any of the check-boxes, then the $_POST array acts like you have no check-boxes at all?!

    So I figured that if I populate $incomingArray with *every* Incoming Message, and then I pass that in the $_POST, then all I need to do is determine if the Top Check-box was checked, and then use that list to update things.

    Unfortunately, I am having troubles with passing this array in my $_POST.

    Can someone please help me figure this out?!



  2. #2
    Senior Coder
    Join Date
    Feb 2011
    Your Monitor
    Thanked 568 Times in 555 Posts
    Quote Originally Posted by doubledee View Post
    As I found out the hard way, even though I might have 20 check-boxes corresponding to 20 Incoming Messsages, if you don't check any of the check-boxes, then the $_POST array acts like you have no check-boxes at all?!
    Thats how it is supposed to work. Thats how your email list in yahoo, hotmail etc works when you select only certain messages to delete - the browser only sends the values you checked. It can cause a few head scratching moments to begin with as you expect it to send a true/false for each box instead but you soon get used to it - it's really quite logical.

    The alternative way you could do it if you wanted a guaranteed value to be sent with your top checkbox is use hidden values instead for each item:

    <input type="hidden" name="incomingArray[]" value="1">
    <input type="hidden" name="incomingArray[]" value="3">
    <input type="hidden" name="incomingArray[]" value="4">

    Note that you would need to ignore these if the user doesn't select the top checkbox and just makes a normal selection with the normal checkboxes. You would process the hidden inputs purely if the top checkbox is selected.
    I can't really think of anything to write here now...


Posting Permissions

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