Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2012, 03:07 AM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
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.                         *
    // ***************************************
    
if ($_SERVER['REQUEST_METHOD']=='POST'){
        
// 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,
                        updated_on=NOW()
                        WHERE member_id_to=?
                        AND message_id=?
                        LIMIT 1"
;

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

                
// Bind variables to query.
                
mysqli_stmt_bind_param($stmt1'ii'$sessMemberID$msgID);

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


                // Execute query.
                
mysqli_stmt_execute($stmt1);

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

                }else{ 

Then farther down in my PHP I have...
PHP Code:
    }else{
        
// 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.
                
mysqli_stmt_execute($stmt6);

                
// Store results.
                
mysqli_stmt_store_result($stmt6);

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

                    
// Bind result-set to variables.
                    
mysqli_stmt_bind_result($stmt6$pmID$readOn$flag$fromID$fromUsername$subject$sentOn);

//NEW
                    // 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;

                    }
//End of BUILD INCOMING ARRAY 

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

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

<!-- 
Body Data -->
<
tbody>";

    // Retrieve Messages from Array.
    foreach($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'
                    
/>
                </
td

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?!

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 12-13-2012, 12:15 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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">
etc

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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 AM.


Advertisement
Log in to turn off these ads.