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.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Unhappy Getting an Array from an HTML Form

    Based on http://www.php.net/manual/en/faq.htm...aq.html.arrays I am of the understanding that I should be able to create multiple INPUT elements in an HTML form with the same NAME attribute (plus brackets) and have these elements all appear as one array in the $_POST object in PHP.

    To test this, I've created the following .php page:
    PHP Code:
    <html>
    <body>
    <?php
      var_dump
    ($_POST);
    ?>
    <form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
      foo[0]: <input type='text' name='foo[]' /><br />
      foo[1]: <input type='text' name='foo[]' /><br />
      bar[0]: <input type='text' name='bar[]' value='value1' /><br />
      bar[1]: <input type='text' name='bar[]' value='value2' /><br />
      <input type='submit' />
    </form>
    </body>
    </html>
    Simply hitting Submit on this page (without populating either foo field, or changing either bar field), I get the following as the result of the var_dump($_POST):
    Code:
    array(2) { ["foo"]=>  string(0) "" ["bar"]=>  string(6) "value2" }
    Likewise, if I populate both foo INPUT fields, the value for $_POST["foo"] will just be a string containing the last INPUT element with NAME attribute of "foo[]", not an array containing both "foo[]" inputs.

    Any ideas what I'm doing wrong?

    Thanks in advance.

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by DDaku View Post
    Any ideas what I'm doing wrong?

    Thanks in advance.
    well their names are the same

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by funnymoney View Post
    well their names are the same
    From the PHP docs article I linked at the top of my original post:

    To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:

    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyArray[]" />

    Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyOtherArray[]" />
    <input name="MyOtherArray[]" />

    This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script.

  • #4
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    don't hold me to this but i'm fairly sure you can't array[] text elements they only work on <select> elemments/ check boxes / radiobuttons
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by hinch View Post
    don't hold me to this but i'm fairly sure you can't array[] text elements they only work on <select> elemments/ check boxes / radiobuttons
    Nah it works on <input> tags, type=text or whatever...

    DDaku I didn't see anything out of place just visually scanning through. What browser are you using and can you try this on another? What PHP version? Maybe your HTML isn't valid and that's causing a problem? Not sure...

    Try this sample, see if it works on your install. I just ran it using Firefox 3.5 and it worked fine.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>Test</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        <meta name="generator" content="HAPedit 3.1">
    </head>
    <body>
    <div>
    <?php echo "<pre>".print_r($_POST,true)."</pre>"?>
    </div>
    <form method="post">
    <input type="text" name='test[]' /><br />
    <input type="text" name='test[]' /><br />
    <input type="text" name='test[]' /><br />
    <input type="text" name='test[]' /><br />
    <input type="submit" value="Submit me" />
    </form>
    </body>
    </html>

  • Users who have thanked Fumigator for this post:

    DDaku (09-01-2009)

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Your previous code seems to work fine. Its giving you the type of input as well as its length. If you want the actual values then do what Fumigator showed you.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Your previous code seems to work fine. Its giving you the type of input as well as its length. If you want the actual values then do what Fumigator showed you.
    The vardump should show foo and bar as arrays, not strings. I prefer print_r() but both would work for this purpose.

  • Users who have thanked Fumigator for this post:

    DDaku (09-01-2009)

  • #8
    New Coder
    Join Date
    Aug 2009
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Well thanks greatly Fumigator, at least somebody understood what I was trying to do

    The good news is it works now, the bad news is I didn't change anything (just created a new test page with Fumigator's code, ran it, and then re-ran my test page). I suppose it must have been a caching problem or something (although I swear I was clearing that).

    Regardless, the problem has gone away, so thanks for taking the time to look into it, I really appreciate the sanity check if nothing else.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You're welcome... I appreciate the well constructed question, especially the reference to material you were studying which indicated you were doing the leg work before coming here! (Sadly, so many people don't even know how to read a manual and come here looking for help)

  • #10
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a similar doubt. can u please help?

    Im trying to get multiple occurances of ec and amt to be stored in arrays with those names and then i have to send them to mysql. So i used arrays in html and tried to get them thru $_REQUEST. but it doesnt work. any help?

    (How to retrieve the html array data into php?)

    PHP Code:
    function getdata(){
    $area = $_REQUEST["area"];
    $month = $_REQUEST["month"];
    $entries = $_REQUEST["entries"];
    for($i=0;$i<$entries;$i++)
    {
    ?>
    <form method="post">
    Employee code : <input type="text" name='ec[]'/>
    Deduction amount : <input type="text" name='amt[]'><br/>
    </form>
    <?php
    }
    $ecarray = array($_REQUEST["ec"]);
    $amtarray = array($_REQUEST["amt"]);
    echo 
    "<form><input type='submit' value='submit'/><br></form>";
    return 
    1;
    }
    maybe im missing something very basic
    Last edited by ilight; 09-11-2009 at 05:59 AM.


  •  

    Tags for this Thread

    Posting Permissions

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