149

So I've got this HTML form:

<html>
<head><title>test</title></head>
<body>
    <form action="myurl" method="POST" name="myForm">
        <p><label for="first_name">First Name:</label>
        <input type="text" name="first_name" id="fname"></p>

        <p><label for="last_name">Last Name:</label>
        <input type="text" name="last_name" id="lname"></p>

        <input value="Submit" type="submit" onclick="submitform()">
    </form>
</body>
</html>

Which would be the easiest way to send this form's data as a JSON object to my server when a user clicks on submit?

UPDATE: I've gone as far as this but it doesn't seem to work:

<script type="text/javascript">
    function submitform(){
        alert("Sending Json");
        var xhr = new XMLHttpRequest();
        xhr.open(form.method, form.action, true);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
        var j = {
            "first_name":"binchen",
            "last_name":"heris",
        };
        xhr.send(JSON.stringify(j));

What am I doing wrong?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
kstratis
  • 6,429
  • 9
  • 39
  • 80
  • 1
    Take a look at `$.ajax` and [`serialize`](http://api.jquery.com/serialize/) in the jQuery API. – Rory McCrossan Mar 05 '14 at 10:33
  • 1
    Does it absolutely have to be a JSON object? What structure should the object have? – Anthony Grist Mar 05 '14 at 10:33
  • 1
    @AnthonyGrist Yes it has to be a JSON cause it's addressed toward a ReST service. – kstratis Mar 05 '14 at 10:39
  • 4
    What does “doesn't seem to work” mean? Remember, we can't see your screen. – Dour High Arch May 21 '15 at 00:54
  • 2
    @Konos5 - REST has nothing to do with JSON. It doesn't require that data be in any particular format. – danielm Oct 01 '15 at 20:38
  • In the above example of your code, your json is incorrect. That might be causing an issue. – AmaJayJB May 10 '17 at 09:57
  • If you want to test the security of a web application against CSRF, there is a hack to send JSON using an HTML form: https://stackoverflow.com/questions/19446544/post-request-to-include-content-type-and-json/49706528#49706528 – baptx Apr 07 '18 at 11:05
  • Here's a library that I created to do just this: https://github.com/keithhackbarth/submitAsJSON – keithhackbarth Apr 18 '18 at 04:55
  • Use [enctype](https://darobin.github.io/formic/specs/json/) `
    ` :) P.S. question marked as too broad but it is not, please update question and add answer to it.
    – Cherry Apr 18 '18 at 13:45
  • line xhr.open(form.method, form.action, true); - form where this function should get the variable form? – Cassian Mar 05 '19 at 15:04

8 Answers8

151

Get complete form data as array and json stringify it.

var formData = JSON.stringify($("#myForm").serializeArray());

You can use it later in ajax. Or if you are not using ajax; put it in hidden textarea and pass to server. If this data is passed as json string via normal form data then you have to decode it using json_decode. You'll then get all data in an array.

$.ajax({
  type: "POST",
  url: "serverUrl",
  data: formData,
  success: function(){},
  dataType: "json",
  contentType : "application/json"
});
SachinGutte
  • 6,438
  • 5
  • 32
  • 57
61

HTML provides no way to generate JSON from form data.

If you really want to handle it from the client, then you would have to resort to using JavaScript to:

  1. gather your data from the form via DOM
  2. organise it in an object or array
  3. generate JSON with JSON.stringify
  4. POST it with XMLHttpRequest

You'd probably be better off sticking to application/x-www-form-urlencoded data and processing that on the server instead of JSON. Your form doesn't have any complicated hierarchy that would benefit from a JSON data structure.


Update in response to major rewrite of the question…

  • Your JS has no readystatechange handler, so you do nothing with the response
  • You trigger the JS when the submit button is clicked without cancelling the default behaviour. The browser will submit the form (in the regular way) as soon as the JS function is complete.
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 1
    OK, so how do I fix this? – kstratis Mar 05 '14 at 11:00
  • 2
    @Quentin : In my case I need cross domain POST without domain control. – user2284570 Sep 12 '15 at 18:33
  • 2
    @user2284570 — If you have a new question, then [ask](http://stackoverflow.com/questions/ask) one. – Quentin Sep 12 '15 at 18:43
  • @Quentin : as been already been asked but answered in way only helpful for the op. In fact I found a way to bypass a forum sanitizer to put data: uri inside an attribute *(not applicable for javascript: scheme)* *(the only way to get attribute value’s is to access page source code)*. And I’m looking for way to prove such a situation can harm users *(every action on the site require to read response)*. – user2284570 Sep 12 '15 at 18:54
  • @user2284570 — Why are you asking your question in a comment on an unrelated answer? – Quentin Sep 12 '15 at 18:58
  • @Quentin : not completely unrelated, I found a cross browser way to read answer with html form, however the site require javascript and all actions use ᴊꜱᴏɴ. So sending a JSON object using html form data can solve the thing. – user2284570 Sep 12 '15 at 19:13
  • @user2284570 pass using JSONP for all cross-domain ajax requests – yardpenalty.com Jul 13 '16 at 18:45
  • @yardpenalty — JSONP is a horrible hack. It demands a hell of a lot of trust as it exposes the site reading the data to XSS, and it is very limited. We have CORS now. Don't use JSONP. – Quentin Jul 13 '16 at 18:50
  • Well we use it for RPGLE PGMS located on a different port than HTTP page requests (php) which makes it very difficult to hack since you can't really inject SQL scripts into our PGMs. I appreciate the info. Will do some investigating on CORS. We are doing same server just different port and we trust ourselves, but for Web Services I would like to use CORS. – yardpenalty.com Jul 13 '16 at 19:16
  • 2
    There is a proposal for adding `enctype='application/json'` to the form definition to create JSON data https://www.w3.org/TR/html-json-forms/ – EkriirkE Mar 12 '18 at 04:17
  • 4
    @EkriirkE — Have you read that page? It says, in a massive box with a black and yellow danger stripe around it **Beware. This specification is no longer in active maintenance and the HTML Working Group does not intend to maintain it further.** – Quentin Mar 12 '18 at 09:34
  • 1
    Here's a library that I created that enables this: https://github.com/keithhackbarth/submitAsJSON – keithhackbarth Apr 18 '18 at 04:54
6

You can try something like:

<html>
<head>
    <title>test</title>
</head>

<body>
    <form id="formElem">
        <input type="text" name="firstname" value="Karam">
        <input type="text" name="lastname" value="Yousef">
        <input type="submit">
    </form>
    <div id="decoded"></div>
    <button id="encode">Encode</button>
    <div id="encoded"></div>
</body>
<script>
    encode.onclick = async (e) => {
        let response = await fetch('http://localhost:8482/encode', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                },
        })

        let text = await response.text(); // read response body as text
        data = JSON.parse(text);
        document.querySelector("#encoded").innerHTML = text;
      //  document.querySelector("#encoded").innerHTML = `First name = ${data.firstname} <br/> 
      //                                                  Last name = ${data.lastname} <br/>
      //                                                  Age    = ${data.age}`
    };

    formElem.onsubmit = async (e) => {
      e.preventDefault();
      var form = document.querySelector("#formElem");
     // var form = document.forms[0];

        data = {
          firstname : form.querySelector('input[name="firstname"]').value,
          lastname : form.querySelector('input[name="lastname"]').value,
          age : 5
        }

        let response = await fetch('http://localhost:8482/decode', {
                method: 'POST', // or 'PUT'
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data),
        })

        let text = await response.text(); // read response body as text
        document.querySelector("#decoded").innerHTML = text;
    };
</script>
</html>
Hasan A Yousef
  • 15,770
  • 15
  • 88
  • 140
4

I'm late but I need to say for those who need an object, using only html, there's a way. In some server side frameworks like PHP you can write the follow code:

<form action="myurl" method="POST" name="myForm">
        <p><label for="first_name">First Name:</label>
        <input type="text" name="name[first]" id="fname"></p>

        <p><label for="last_name">Last Name:</label>
        <input type="text" name="name[last]" id="lname"></p>

        <input value="Submit" type="submit">
    </form>

So, we need setup the name of the input as object[property] for got an object. In the above example, we got a data with the follow JSON:

{
"name": {
  "first": "some data",
  "last": "some data"
 }
}
orafaelreis
  • 2,417
  • 2
  • 24
  • 31
2

you code is fine but never executed, cause of submit button [type="submit"] just replace it by type=button

<input value="Submit" type="button" onclick="submitform()">

inside your script; form is not declared.

let form = document.forms[0];
xhr.open(form.method, form.action, true);
tdjprog
  • 631
  • 6
  • 10
1

The accepted answer is out of date; nowadays you can simply add enctype="application/json" to your form tag and the browser will jsonify the data automatically.

The spec for this behavior is here: https://www.w3.org/TR/html-json-forms/

prideout
  • 2,598
  • 1
  • 18
  • 22
  • 2
    No browser is currently having this behavior. This functionality must be obsolete or not implemented at all. Its a cool feature though. – Ponmudi VN Dec 29 '20 at 06:32
  • 3
    **The `enctype=application/json` feature is abandoned** - see https://stackoverflow.com/questions/38017123/is-form-enctype-application-json-available – mikemaccana Dec 31 '20 at 09:57
0

I found a way to pass a JSON message using only a HTML form.

This example is for GraphQL but it will work for any endpoint that is expecting a JSON message.

GrapqhQL by default expects a parameter called operations where you can add your query or mutation in JSON format. In this specific case I am invoking this query which is requesting to get allUsers and return the userId of each user.

{ 
 allUsers 
  { 
  userId 
  }
}

I am using a text input to demonstrate how to use it, but you can change it for a hidden input to hide the query from the user.

<html>
<body>
    <form method="post" action="http://localhost:8080/graphql">
        <input type="text" name="operations" value="{&quot;query&quot;: &quot;{ allUsers { userId } }&quot;, "variables":  {}}"/>
        <input type="submit" />
    </form>
</body>
</html>

In order to make this dynamic you will need JS to transport the values of the text fields to the query string before submitting your form. Anyway I found this approach very interesting. Hope it helps.

Israelm
  • 1,442
  • 2
  • 18
  • 26
0

The micro-library field-assist does exactly that: collectValues(formElement) will return a normalized json from the input fields (that means, also, checkboxes as booleans, selects as strings,etc).

Avi Tshuva
  • 241
  • 2
  • 12