Results 1 to 3 of 3

Thread: posting an array on submit

  1. #1

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    posting an array on submit

    I have an array that I need to submit to a form. An array does not fit into any of the known form elements. How can I fit this array into scheme of forms and submit it?
    Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: posting an array on submit

    arrays on forms work like this (if submitted via post, otherwise use $_GET):

    PHP Code:
    //the following produces the variable $_POST['array']['key'] with a value of "test"
    <input type="hidden" name="array[key]" value="test" />

    //the following produces the variable $_POST['array'][0] through $_POST['array'][2] with a value of "test"
    <input type="hidden" name="array[]" value="test" />
    <
    input type="hidden" name="array[]" value="test" />
    <
    input type="hidden" name="array[]" value="test" /> 
    so, if you have your array, you can simply loop through it using a foreach(), and then put it into hidden elements.
    PHP Code:
    <?php foreach($myarray as $key => $value): ?>
    <input type="hidden" name="myarray[<?php echo $key?>]" value="<?php echo htmlentities($value); ?>" />
    <?php endforeach; ?>
    the above code would produce an array that could be accessed via the same keys as the original array, but the "root" array would be $_POST['myarray'] rather than $myarray. for simplicity's sake, you could redefine it to use $myarray after you had submitted the form.

    hope that was what you were looking for!

  3. #3

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    Resolved Re: posting an array on submit

    Yes, this is what I was looking for. Thanks!
    Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic

Posting Permissions

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



Click Here to Expand Forum to Full Width