#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Question How to get multi-part data out of POST array


    I have an HTML form posting data to a PHP script.

    Html code below loads to a multidimension array with this code:

    <table border="2" style="background:#ececec" align ="center" width="707" cellpadding="2" bordercolor="#008000">
    <tr><td width="31" align="center">
    <input type="checkbox" name="selections[ItemName1][ItemPrice1]" value="" >
    </td><td width="354">First Item Name</td>
    <td width="98" align="right">$100.00</td><td align="center">More Info</td></tr>

    <tr><td width="31" align="center">
    <input type="checkbox" name="selections[ItemName2][ItemPrice2]" value=""></td>
    <td width="354">Second Item Name</td>
    <td width="98" align="right">$200.00</td>
    <td align="center">More Info</td>
    </tr>
    </table>

    PHP code shown below returns
    Array ( [ItemName1] => Array ( [ItemPrice1] => ) [ItemName2] => Array ( [ItemPrice2] => ) )

    <?php
    print_r($_POST['selections']);
    ?>

    Can someone tell me how to extract the ItemName and ItemPrice values. I want to build an HTML table with them but can't figure out how to get them out of the POST

    Using arrayname[0][0] or [0][1] doesn't work, and I've tried a zillion other ways........
    Appreciate any help with this
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    Using an associative array rather than an indexed array may not have been the best choice. However, to get the keys in your loop use this form:
    PHP Code:
    $selections=$_POST['selections'];
    foreach (
    $selections as $key=>$value) {
       echo 
    "$key is $value<br />";

    Your 2 dimensional array is probably not needed.
    Note the use of [ PHP ] tags. See the sticky at the top of this forum.
    Last edited by gw1500se; March 7th, 2013 at 03:50 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Question


    Originally Posted by gw1500se
    Using an associative array rather than an indexed array may not have been the best choice. However, to get the keys in your loop use this form:
    PHP Code:
    $selections=$_POST['selections'];
    foreach (
    $selections as $key=>$value) {
       echo 
    "$key is $value<br />";

    Your 2 dimensional array is probably not needed.
    Note the use of [ PHP ] tags. See the sticky at the top of this forum.
    I noted "use of [ PHP ] tags" sticky. will do next time.
    The code you provided is where I've been last couple days. It gets values of key as ItemName1 and ItemName2 BUT does not get values of Price1 and Price2. I need to load both into a table to look something like.

    ItemName1 Price1
    ItemName2 Price2 etc, etc.

    Jim
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    Like I said, you may not need the 2 dimensional array. It is not clear from what you posted why you think you need the array the way you have it. Perhaps if you explain, we can suggest a simpler way to do it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    Like I said, you may not need the 2 dimensional array. It is not clear from what you posted why you think you need the array the way you have it. Perhaps if you explain, we can suggest a simpler way to do it.
    Hey gw1500se - Thanks for the attempt and the offer. I have an HTML which allows the user to pick between several items (item1 & price1, etc.). This form then submits (via POST) to a secure PHP where I am simply trying to load the items passed into a table and sum up the balance for the user to view before submitting his address and CC info. I can easily get the item names, but cannot seem to get to the prices. I thought a multi-dimensional array would be the simplist way. (FYI - I'm an ex Clipper programmer and in Clipper - similar to C - that would be an easy chore for me).

    Jim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    Are the prices not tied to a specific item? If so use the item name as the key and the price as the value.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    Are the prices not tied to a specific item? If so use the item name as the key and the price as the value.
    That's what I've been trying to do. Can you give me a line of PHP code that will extract both item and price to $vars? I have tried about a zillion combos with zero success!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    Are the prices not tied to a specific item? If so use the item name as the key and the price as the value.
    If you want, I'll give you the website address of my test form and PHP receiver and you can run my test code and see what I'm saying.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    The problem is not with PHP. It is how you are setting up your form for selections. Is your form static or generated by PHP. If the latter, post the code you use to generate it. If the former, post the form HTML.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    The problem is not with PHP. It is how you are setting up your form for selections. Is your form static or generated by PHP. If the latter, post the code you use to generate it. If the former, post the form HTML.
    It's a static HTML form generated in FrontPage 2003 here's the entire HTML code.

    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Test send</title>
    </head>
    <body>
    <u><b><font face="Arial" size="2">Test Data Send to PHP</font></b></u></p>
    <form action="test_receive.php" method="post">
    <div align="center">
    <table border="2" style="background:#ececec" align ="center" width="707" cellpadding="2" bordercolor="#008000">
    <tr><td width="31" align="center">
    <input type="checkbox" name="selections[ItemName1][ItemPrice1]" value="" >
    </td><td width="354">First Item Name</td>
    <td width="98" align="right">$100.00</td><td align="center">More Info</td></tr>

    <tr><td width="31" align="center">
    <input type="checkbox" name="selections[ItemName2][ItemPrice2]" value=""></td>
    <td width="354">Second Item Name</td>
    <td width="98" align="right">$200.00</td>
    <td align="center">More Info</td>
    </tr>
    </table>
    <!-- Submit button here -->
    <p align="center">&nbsp;<b><font color="#FF0000">
    <input type="submit" value="Submit Order" style="float: center"></font></b>
    </p>
    </form>
    </div>
    </table>
    </body>
    </html>
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    The problem is not with PHP. It is how you are setting up your form for selections. Is your form static or generated by PHP. If the latter, post the code you use to generate it. If the former, post the form HTML.
    Well that was a clue, and after several hours of playing, I finally got it to work. Unfortunately, while I am an expert programmer in the strongly typed language of Clipper, with a few published books, I know little about PHP. Syntax is similar to what I'm used to, but different enough to cause me lots of grief.

    Anyway, it's working now. I modified both ends. Thanks for your effort!

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo