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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Location
    U.K.
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Send JSON array alongside HTML form

    I have a basic form as follows:

    Code:
    <form action="myForm_submit.php" method="post" accept-charset="utf-8" name="myForm">
    <input type = "text" name="firstName" />	
    <input type = "text" name="lastName" />	
    
    <input type="submit" value="Submit">
    </form>
    And I have a javascript array:

    Code:
    myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
    When I hit submit and send my form data I would like to send myArray in JSON format to my php submit file. How would I do that and how would I code the php $_POST = ..... for my JSON array?

  2. #2
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,095
    Thanks
    29
    Thanked 238 Times in 236 Posts
    Code:
    <?PHP
    echo $_REQUEST["aray"];
    ?>
    
    <form method="post" accept-charset="utf-8" name="myForm" onsubmit="addJSON(aray)">
    <input type = "text" name="firstName" />	
    <input type = "text" name="lastName" />	
    <input type = "hidden" name="aray" >
    <input type="submit" value="Submit">
    </form>
    
    <script>
    myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
    function addJSON(el){
    	el.value=JSON.stringify(myArray)
    }
    </script>
    Last edited by DaveyErwin; 08-31-2012 at 08:04 PM.

  3. #3
    New to the CF scene
    Join Date
    Dec 2011
    Location
    U.K.
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the reply.
    Code:
    myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
    is just an example to simplify what I am trying to do. I actually have a large form with hundreds of fields to send. That's why I wanted to do it with one JSON array and not with hundreds of hidden input fields.

  4. #4
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,095
    Thanks
    29
    Thanked 238 Times in 236 Posts
    Quote Originally Posted by russthebarber View Post
    Thanks for the reply.
    Code:
    myArray = ["bananas", "apples", "pears", "peaches", "oranges"];
    is just an example to simplify what I am trying to do. I actually have a large form with hundreds of fields to send. That's why I wanted to do it with one JSON array and not with hundreds of hidden input fields.
    so uh well these fields ?
    are not form elements ?
    that the user types in ?
    where do they come from ?
    Last edited by DaveyErwin; 08-31-2012 at 08:09 PM.

  5. #5
    New to the CF scene
    Join Date
    Dec 2011
    Location
    U.K.
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No, they are not form elements. They come from two large javascript arrays. I need to send all this data with the form. The form alone has about 100 elements at the moment and the arrays both have about 150 elements.


 

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
  •