1


I have a html form like this:

<form action="/create" method="POST">

    <input type="text" placeholder="title" name="title">
    <input type="text" placeholder="year" name="toYear">
    <input type="text" placeholder="genre" name="genre">
    <input type="text" placeholder="director" name="director">    
    <input type="url" placeholder="poster" name="poster">
    <textarea name="plot" placeholder="plot..." cols="30" rows="10"></textarea>            

    <div id="actors">
        <h5>Actors</h5>
        <button type="button" id="addActor">Add Actor</button>
    </div>

    <div id="ratings">
        <h5>Ratings</h5>
        <button type="button" id="addRating">Add Rating</button>
    </div>
<button type="submit">Add to Collection
</button>
</form>

And my script below it:

document.getElementById('addRating').addEventListener('click', function () {

    var ratingsElem = document.getElementById('ratings');

    var sourceElem = document.createElement("input");
    sourceElem.setAttribute('type', 'text');
    sourceElem.setAttribute('placeholder', 'Source');
    sourceElem.setAttribute('name', 'ratingsSource');

    var valueElem = document.createElement("input");
    valueElem.setAttribute('type', 'text');
    valueElem.setAttribute('placeholder', 'value');
    valueElem.setAttribute('name', 'ratingsValue');


    ratingsElem.appendChild(sourceElem);
    ratingsElem.appendChild(valueElem);

})

document.getElementById('addActor').addEventListener('click', function () {

    var ActorsElem = document.getElementById('actors');

    var actorElem = document.createElement("input");
    actorElem.setAttribute('type', 'text');
    actorElem.setAttribute('placeholder', 'Name');
    actorElem.setAttribute('name', 'actors');

    ActorsElem.appendChild(actorElem);
});

Everything goes well on submit but I want to make the Ratings values into an array of rating objects like this:

"Ratings": [
{
  "Source": "Internet Movie Database",
  "Value": "8.8/10"
},
{
  "Source": "Rotten Tomatoes",
  "Value": "91%"
},
{
  "Source": "Metacritic",
  "Value": "92/100"
}
  ]

and append it to form data sent to server. How may I achieve this? ........................................

2 Answers 2

1
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"
});

Reference : How to send a JSON object using html form data

Sign up to request clarification or add additional context in comments.

Comments

1

There is few ways this can be done, but here is the simpliest one

var rating = 0;

document.getElementById('addRating').addEventListener('click', function () {
  var ratingsElem = document.getElementById('ratings');

  var sourceElem = document.createElement("input");
  sourceElem.setAttribute('type', 'text');
  sourceElem.setAttribute('placeholder', 'Source');
  sourceElem.setAttribute('name', 'Ratings[' + rating + '][Source]');

  var valueElem = document.createElement("input");
  valueElem.setAttribute('type', 'text');
  valueElem.setAttribute('placeholder', 'value');
  valueElem.setAttribute('name', 'Ratings[' + rating +  '][Value]');

  ratingsElem.appendChild(sourceElem);
  ratingsElem.appendChild(valueElem);

 rating++;

});

If you want to do it without adding variable rating, on each new input for source you can calculate his index and than add input value with same index...

I hope this was helpful...

Here is output when I tested:

{
  "title": "",
  "toYear": "",
  "genre": "",
  "director": "",
  "poster": "",
  "plot": "",
  "Ratings": [
    {
      "Source": "1",
      "Value": "2"
    },
    {
      "Source": "2",
      "Value": "3"
    }
  ]
}

2 Comments

wow. this is a smart and easy way(though it's limited to this example)
why you think it's limited to this example? please explain so I can make it more universal to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.