Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Array only returning one value?

    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 script 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

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <input name='testArray[]' type='checkbox' value='TRUE' />
    This is HTML - it has no knowledge of the PHP array.

    PHP Code:
    <?php
    foreach ($testArray as $value) {
        echo 
    "<input name='testArray[]' type='hidden' value='$value' />";
    }
    ?>
    (untested currently)
    ..and on the receiving end:
    PHP Code:
    <?php
    if ($_POST['testArray'] && !empty($_POST['testArray'])) {
        
    $testArray $_POST['testArray'];
    }
    ?>
    Last edited by AndrewGSW; 12-15-2012 at 06:58 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Code:
    <input name='testArray[]' type='checkbox' value='TRUE' />
    This is HTML - it has no knowledge of the PHP array.
    If I had a Form field called "username", and my HTML had this code...
    Code:
        <input name='username' type='text' value=$username />";
    ...then when the Form was submitted, my PHP script would be able to handle what was in $_POST['username'].

    And as far as I know, that should apply to the code I provided as well.


    Quote Originally Posted by AndrewGSW View Post
    PHP Code:
    <?php
    foreach ($testArray as $value) {
        echo 
    "<input name='testArray[]' type='hidden' value='$value' />";
    }
    ?>
    (untested currently)
    I'm sorry, but I'm not understanding what you are recommending...


    Debbie

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Quote Originally Posted by doubledee View Post
    Why isn't my $testArray being returned successfully in the $_POST array with all 11 values like described above?
    Because you need to define in the html what those 11 values are. Currently you've defined that entire array as having one value - 'True'. That is what the browser will send and that is what PHP will read from the form and recognise. It will not use the field name to read values from another script, it will only read the values that the form sent.

    What you need to do is as Andrew says - define each element of the array in the form manually giving each element the same array name and then it's own unique value. This will then be submitted back to the server as an array with those values.

    Alternatively, you could store the values in a session instead and simply put the name of the session key in the form:

    PHP Code:
    session_start();

    //******************
    //Your code - returning $testArray
    //******************

    $RandomName uniqid();  //Use random key name to avoid duplicating data
    $_SESSION[$RandomName] = $testArray
    Then in your html output:
    <input name='data' type='hidden' value='<?php print $RandomName; ?>' />

    Then in your processing script:
    PHP Code:
    session_start();

    $testArray $_SESSION[$_POST['data']]; 
    Last edited by tangoforce; 12-15-2012 at 07:35 PM. Reason: Added comment for random key name.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    doubledee (12-16-2012)

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <input name='username' type='text' value=$username />";
    This is only a code-fragment. It is more likely to be:

    PHP Code:
    <?php
    echo "<input name='username' type='text' value=$username />";
    ?>
    ..and when you view the source for the (HTML) page that is constructed, it will show as:
    Code:
    <input name='username' type='text' value=bobbiscuit />
    The code I showed will generate HTML source as:
    Code:
    <input name='testArray[]' type='hidden' value='52' />
    <input name='testArray[]' type='hidden' value='51' />
    <input name='testArray[]' type='hidden' value='49' />
    <!-- etc.. -->
    which, when submitted to PHP, will be received as a $_POST array.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Quote Originally Posted by doubledee View Post
    If I had a Form field called "username", and my HTML had this code...
    Code:
        <input name='username' type='text' value=$username />";
    ...then when the Form was submitted, my PHP script would be able to handle what was in $_POST['username'].
    Because you gave that form element a value which used a PHP variable - $username. PHP will replace that variable when serving the page with its actual value so it would look like 'Joe Bloggs' in the html source instead of $username.

    Quote Originally Posted by doubledee View Post
    And as far as I know, that should apply to the code I provided as well.
    Yes but you gave it a fixed constant - the word 'True'. You need to set it to the value of your php variable as you yourself demonstrated above.


    Quote Originally Posted by doubledee View Post
    I'm sorry, but I'm not understanding what you are recommending...
    Quote Originally Posted by AndrewGSW View Post
    PHP Code:
    <?php
    foreach ($testArray as $value) {
        echo 
    "<input name='testArray[]' type='hidden' value='$value' />";
    }
    ?>
    The code Andrew has given you would output this:

    <input name='testArray[]' type='hidden' value='52' />
    <input name='testArray[]' type='hidden' value='51' />
    <input name='testArray[]' type='hidden' value='49' />
    <input name='testArray[]' type='hidden' value='39' />
    <input name='testArray[]' type='hidden' value='38' />
    <input name='testArray[]' type='hidden' value='10' />
    <input name='testArray[]' type='hidden' value='8' />
    <input name='testArray[]' type='hidden' value='6' />
    <input name='testArray[]' type='hidden' value='5' />
    <input name='testArray[]' type='hidden' value='2' />
    <input name='testArray[]' type='hidden' value='1' />

    When submitted via post, those values will be in $_POST['testArray']
    Last edited by tangoforce; 12-15-2012 at 07:30 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    doubledee (12-16-2012)

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    tangoforce's solution is preferable if you are passing a large amount of data between pages, or doing so frequently.
    Last edited by AndrewGSW; 12-15-2012 at 07:28 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    I would like to point out that there is only ONE script. (My php script generates the data, then displays the Form, and when the Form gets submitted, the same script does the processing.

    Also, Andrew, you updated your response after I replied.

    I am re-reading what you said and trying to process this with my tiny brain!


    Debbie

  • #9
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Code:
    <input name='username' type='text' value=$username />";
    This is only a code-fragment. It is more likely to be:

    PHP Code:
    <?php
    echo "<input name='username' type='text' value=$username />";
    ?>
    ..and when you view the source for the (HTML) page that is constructed, it will show as:
    Code:
    <input name='username' type='text' value=bobbiscuit />
    The code I showed will generate HTML source as:
    Code:
    <input name='testArray[]' type='hidden' value='52' />
    <input name='testArray[]' type='hidden' value='51' />
    <input name='testArray[]' type='hidden' value='49' />
    <!-- etc.. -->
    which, when submitted to PHP, will be received as a $_POST array.

    Some comments/questions...

    1.) Aren't there security risks using HIDDEN??

    2.) Why can't I take my php variable $testArray which is already holding all 11 values that I need, and pass that variable/array in place of value in this code...
    Code:
    	<input name='testArray[]' type='checkbox' value=<?php echo $testArray ?> />

    Debbie

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Quote Originally Posted by doubledee View Post
    Some comments/questions...

    1.) Aren't there security risks using HIDDEN??
    Not really as long as you check that they are the correct data type / escape before using in a query.

    Quote Originally Posted by doubledee View Post
    2.) Why can't I take my php variable $testArray which is already holding all 11 values that I need, and pass that variable/array in place of value in this code...
    Code:
    	<input name='testArray[]' type='checkbox' value=<?php echo $testArray ?> />
    Because HTML doesn't allow you to declare an array like that with one variable. If you want to declare an array, you must do it the old fashioned way - declaring one item at a time. Sorry, thats just the way that html was setup to work.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Quote Originally Posted by doubledee View Post
    I would like to point out that there is only ONE script. (My php script generates the data, then displays the Form, and when the Form gets submitted, the same script does the processing.

    That doesn't matter. The same still applies, you just use both techniques in the same script instead. The only difference is that the array you created the first time it ran will not be available the second time (unless you create it both times before the code splits for processing / form output) because they are different instances and the old memory (variables etc) is lost when it stops running the first time hence the use of sessions.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Because HTML doesn't allow you to declare an array like that with one variable. If you want to declare an array, you must do it the old fashioned way - declaring one item at a time. Sorry, thats just the way that html was setup to work.
    So if I was using OOP, and needed to pass an Object in an HTML Form - not that I am - then there is no way to do that?


    Also, I want to clarify something...

    I am trying to make it so if the user checks the Top Check-box in the Inbox Header, and then chooses an action like "Mark as Unread" and then presses "Go", that I send every PM in my Inbox to my UPDATE query to make change the status of every PM.

    If I am using the HIDDEN approach mentioned above, then will that still work for what I need? That is, I really only care about the 11 PM's and their PM ID's if the top check-box is checked.

    (Just asking in advance while I try to apply the suggestions above...)

    Thanks,


    Debbie

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Quote Originally Posted by doubledee View Post
    So if I was using OOP, and needed to pass an Object in an HTML Form - not that I am - then there is no way to do that?
    No unforunately there isn't. With an array (and I've just thought of this) you could actually run it through seizlize() and then use the returned string as your form value instead. Just be sure to run it back through unserialize() when the form is submitted so that it is turned back into a php array.

    UNTESTED (but in theory should work):
    PHP Code:
    $Serialized serialize($testArray);

    //Assuming this is for your top checkbox
    print "<input type=\"checkbox\" name=\"top\" value=\"$Serialized\">"
    Then..
    PHP Code:
    $testArray unserialize($_POST['top']); 
    AFAIK, serizlize and unserialize don't work on objects.

    Quote Originally Posted by doubledee View Post
    Also, I want to clarify something...

    I am trying to make it so if the user checks the Top Check-box in the Inbox Header, and then chooses an action like "Mark as Unread" and then presses "Go", that I send every PM in my Inbox to my UPDATE query to make change the status of every PM.

    If I am using the HIDDEN approach mentioned above, then will that still work for what I need?.
    As long as the Ids are of all the messages in your html list then yes. As I mentioned in your other thread, you could either use seperate hidden elements with all the values or you coud use a session or serialize the array and put it in the form field (which would mean you only need ONE field).
    Last edited by tangoforce; 12-15-2012 at 08:35 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    That doesn't matter. The same still applies, you just use both techniques in the same script instead. The only difference is that the array you created the first time it ran will not be available the second time (unless you create it both times before the code splits for processing / form output) because they are different instances and the old memory (variables etc) is lost when it stops running the first time hence the use of sessions.
    That is the whole point of why I am doing this...

    When my script first runs, I grab the UserID, then go to my database and pull up all Incoming PM's for that UserID, and then store them in an array.

    From there, I drop down to my HTML Form, and I display all of the User's Incoming PM's.

    If she wants to make PM #1, 2, 5, and 7 as "Unread", then she would check those Check-boxes, and submit the Form. The Form then places those checked Check-boxes in the $_POST array, which my same php script picks up and runs an UPDATE query on.

    However, if the user doesn't want to have to check all, say 50, PM's to mark them all as "Unread", then I want to give her the ability - without using JavaScript - to just check the Top Check-box, and my script will update all 50 PM's.

    Where the problem exists is that while I can populate some array with all 50 PM's when my script first runs (e.g. $testArray), when the user submits the Form, any values in my array get erased.

    So I figured that if I could pass the entire array back via the $_POST array, then I could continue to use $testArray, and use all 50 values in it to run my UPDATE and thus mark all 50 PM's as "Unread", if you follow me?!


    Debbie

  • #15
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    AFAIK, serizlize (sic) and unserialize don't work on objects.
    I assume you are referring to JS stringify() and parse()? Serializing in PHP is intended to serialize objects..? Added: to pass a simple array of ids (integers) as a string I would prefer implode() and explode() which have less overhead than serialize().

    If passing OOP objects between pages you are likely to store them as serialized session data, as indicated by tangoforce's recent post.

    @Debbie HIDDEN only means hidden from view on the page - it is completely visible/accessible when posted to PHP.
    Last edited by AndrewGSW; 12-15-2012 at 08:49 PM. Reason: accessible
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    doubledee (12-16-2012)


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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