www.webdeveloper.com
Results 1 to 2 of 2

Thread: dynamically append form data to a post request

  1. #1
    Join Date
    Oct 2011
    Posts
    4

    dynamically append form data to a post request

    I am analyzing header tags for submitting forms online in order to better understand how htmlhttp forms work- signing in to gmail, yahoo mail, and amazon, using the network tab of google developer tools, to view the http headers of requests and responses, and learn where they come from. I am seeing very peculiar behavior in my browser's (chrome)request headers when signing into amazon. The form data, always has the following two header names appended to the second and third to last places of the form data query: x=(some random number), and y=(some random number).

    There are no input tags getting dynamically generated, since when i tell google chrome to break on all subtree modifications, the site still sends off the post with the added parameters before anything gets modified, BEFORE crashing.

    There also is NO ajax call which is causing dynamically added data to be appended to the form post.

    My question is this: is there any way, using javascript, to instruct a browser, to append data as data, to a form post, WITHOUT the use of dynamically appended input elements, or ajax?

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    There is no way to provide pure binary data in the POST, however with the combination of JSON and a server side script able to parse JSON into a hash array, you can fake it using a hidden form field.

    First, go to http://www.json.org/ and download json2.js, this provides the ability for JavaScript to safely encode and decode a JSON string and guards against cross site scripting attacks. Now you can pass arbitrarily deep data structures to the backend:

    JavaScript/HTML
    Code:
    <script>
    var data = {
      title: "Jobs",
      names: ["John", "Bill", "Stacey"],
      positions: [
        {title: "Engineer", id: 1},
        {title: "Manager", id: 2}
      ]
    };
    
    document.getElementById("my_form").onclick = function() {
      this.elements.json.value = JSON.stringify(data);
    };
    </script>
    
    <form id="my_form">
       ... visible form fields
      <input type="hidden" name="json">
    </form>
    And on the server side:

    PHP
    Code:
    $data = json_decode($_POST['json']);
    echo $data['title'];
    echo $data['names'][1];
    echo $data['positions'][0]['title'];

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