www.webdeveloper.com
Results 1 to 11 of 11

Thread: Is this possible to $_POST?

  1. #1
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687

    Is this possible to $_POST?

    I want to know if I can, through the $_POST method, send a multi-dimensional array.

    If not, how can I send a multidimensional array from one PHP page to another?

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Unless I'm missing something obvious, in any multiple selection form element there is only one name or id so would you need more than 1D?

    Just append [] to the form element name and it ends up as $_POST['name'] as an array after submit, i.e.

    Code:
    <input type='checkbox' name='options[]' value='1' /> Item one
    <input type='checkbox' name='options[]' value='2' /> Item two
    PHP Code:
    print_r($_POST['options']); 
    Aside from form submission, you can pass arrays of any dimension using session variables from page to page if you wish. Just remember -- unlike a real PHP multidimensional array, $_SESSION keys at the root level must be valid variable names, not numbers.

    -jim

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Within the same domain, the session option suggested above is probably more efficient than sending the data via post. But if you do need to send it via post for some reason (such as a cURL request to another site or something), you can serialize() the array and send the resulting string as a single post field, then unserialize() it in the receiving script.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Quote Originally Posted by SrWebDeveloper View Post
    Unless I'm missing something obvious, in any multiple selection form element there is only one name or id so would you need more than 1D?
    Yes. I am generating an XML page, and what is entered on the form on one page dictates if the generated XML reads:

    HTML Code:
    			<BoxScore
    				p_id="PL_Yalenchka_Kasa"
    				g_pos="START"
    				ch_pos="G"
    				min="39:14"
    				fg="11/20"
    				thrpt="3/5"
    				ft="4/4"
    				plmn="-14"
    				off="0"
    				reb="4"
    				ast="7"
    				to="5"
    				stl="2"
    				bs="0"
    				ba="2"
    				pf="3"
    				pts="29"
    				top="TOP"
    			/>
    or

    HTML Code:
    			<BoxScore
    				p_id="PL_Yalenchka_Kasa"
    				g_pos="BENCH"
    				dnp="Coach's Decision"
    			/>
    It's for a fantasy basketball site; dnp stands for "Did not play". The keys of the sent array will pop up as the names of elements and attributes.

  5. #5
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    edit: Got distracted while posting so I missed the details, but this should help some anyway.

    In general the answer is yes, for example if you have
    HTML Code:
         <input name="full_name[]" type="text" id="full_name[]" value="<?php echo $value0; ?>" />
         <input name="year_salary[]" type="text" id="year_salary[]" value="<?php echo $value1; ?>" />
         <input name="job_title[]" type="text" id="job_title[]" value="<?php echo $value2; ?>" />
         <input name="co_name[]" type="text" id="co_name[]" value="<?php echo $value3; ?>" />
    and you wanted to pull just the "full_name" you can
    PHP Code:
    foreach ($_POST['full_name'] as $name) {
        echo 
    $name."\n";

    and you get
    Aaron Whistler Jr.
    Wendy Buttons
    and if you want to pull the "full_name" AND "year_salary" AND "job_title" AND "co_name" you can do this by using one of them to get the indexes
    PHP Code:
    for ($i=0$i count($_POST['full_name']); $i++) { 
        echo 
    $_POST['full_name'][$i]." earns ";
        echo 
    $_POST['year_salary'][$i]." per year as ";
        echo 
    $_POST['job_title'][$i]." at ";
        echo 
    $_POST['co_name'][$i].".\n";

    and you get
    Aaron Whistler Jr. earns $1,000,000 per year as assistant mailboy at Whoopee Incorporated.
    Wendy Buttons earns $40,000 per year as SysAdmin at WeRtheBest DotCOMM.
    But, there are other options depending on what you need to do.
    Last edited by opifex; 11-29-2009 at 03:20 AM.
    ...but stupidity is terminal.

  6. #6
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    I did figure out a way around it: Create a seperator.

    PHP Code:
    define('sep' '|');
    $inf = array(
        
    'Game' => array(),
        
    'Home' => array(),
        
    'Away' => array()
    );


    foreach(
    array_keys($_POST) as $pkey){
        
    $sep1 strpos($pkeysep);
        
    $d1 substr($pkey0$sep1);
        
    $d2 substr($pkey, ($sep1+1));
        if(
    strpos($d2sep)){
            
    $sep2 strpos($pkeysep, ($sep1+1));
            
    $d2 substr($pkey, ($sep1+1), ($sep2 $sep1 1));
            
    $d3 substr($pkey, ($sep2 1));
            
    $inf[$d1][$d2][$d3] = $_POST[$pkey];
        } else {
            
    $inf[$d1][$d2] = $_POST[$pkey];
        }


  7. #7
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    Like they say, "There's more than one way to skin a cat."

    But I never could figure out why somebody would want to skin a cat in the first place.
    ...but stupidity is terminal.

  8. #8
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I'd serialize vs. using the separator method.

  9. #9
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    @Mr Initial Man

    Since you're generating an xml anyway, you're way ahead of the game!
    Just parse the xml for your stats box info... no sense in doing more work than you have to.

    ...or am I missing something?
    ...but stupidity is terminal.

  10. #10
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,687
    Quote Originally Posted by SrWebDeveloper View Post
    I'd serialize vs. using the separator method.
    How do you do that with a bunch of form names?

    Quote Originally Posted by opifex View Post
    @Mr Initial Man

    Since you're generating an xml anyway, you're way ahead of the game!
    Just parse the xml for your stats box info... no sense in doing more work than you have to.

    ...or am I missing something?
    Okay, what's going on is this is part of a larger update setup: guys who know how to do statistics but not XML use the form to in the numbers. The script generates the XML. They send me the XML, and I add it to a larger XML file.

  11. #11
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Quote Originally Posted by Mr Initial Man View Post
    How do you do that with a bunch of form names?
    Refer to reply #3 for command help - If you have XML multi-dimensional array data prior to generating the form serialize it and pass it as one value, the on form submit in your processing script unserialize and you get back the original data.

    -jim

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles