0

HTML CODE

<form id="details">
  <div>
    <input type="text" placeholder="Email ID" id="email"></input>
    <input type="text" placeholder="Mobile Number" id="mobile"></input>
</div>
<h5>Data</h5>
<div>
    <div>
        <input type="text" placeholder="Name" id="name1"></input>
        <input type="text" placeholder="Age" id="age1"></input>
    </div>
    <div>
        <input type="text" placeholder="Name" id="name2"></input>
        <input type="text" placeholder="Age" id="age2"></input>
    </div>
</div>
</form>

JS CODE

$(document).ready(function() {
    $("form").on('submit', function(e) {
        // Prepare data
        var form = $("form");
        var formData = new FormData(document.getElementById("details"));
        e.preventDefault();
        $.ajax(form.attr('action'), {
            type: 'POST',
            data: formData,
            contentType: false,
            cache: false,
            processData:false,
            dataType: "json",
            success: function(result) {
                // Success Code
            },
            error: function(result) {
                // Failure Code
            },
            timeout: 5000,
        });
    });
});

The codepen link for my code is http://codepen.io/anon/pen/VKzGRG

I want to send data like

{
    "email" : "[email protected]",
    "mobile" : "9898989898",
    "data" : [
        {
            "name":"xyz",
            "age":45
        },
        {
            "name":"xyz",
            "age":45
        }
    ]
}

I tried sending the data using jQuery.

The problem is that it's sending only one name and age.

Also, in my project, I'm dynamically adding a Name and Age box using jQuery and a button.

How can I send the data using AJAX post method using jQuery?

4
  • Codepen is not working + What is your error with your ajax call ? Commented Sep 30, 2016 at 8:27
  • I dont know how to send data using JSON in the format I mentioned Commented Sep 30, 2016 at 8:28
  • Possible duplicate of Serialize form data to JSON Commented Sep 30, 2016 at 8:30
  • 1
    haven´t you asked this Q 10 mins before? oO Commented Sep 30, 2016 at 8:30

2 Answers 2

1

you have missed the name of input that's why data is not posting see the below working code.

//HTML code

<form  name="details" action="t.php" id="details">
  <div>
    <input type="text" name="email" placeholder="Email ID" id="email"></input>
    <input type="text" name="mobile" placeholder="Mobile Number" id="mobile"></input>
</div>
<h5>Data</h5>
<div>
    <div>
        <input type="text" name="data1[]" placeholder="Name" id="name1"></input>
        <input type="text" name="data1[]" placeholder="Age" id="age1"></input>
    </div>
    <div>
        <input type="text" name="data2[]" placeholder="Name" id="name2"></input>
        <input type="text" name="data2[]" placeholder="Age" id="age2"></input>
    </div>
</div>
<input type="submit" value="send">
</form>

//ajax code

$(document).ready(function() {
    $("form").on('submit', function(e) {
        // Prepare data
        var form = $("form");
        var formData = new FormData(document.getElementById("details"));
        console.log(formData);
        e.preventDefault();
        $.ajax(form.attr('action'), {
            type: 'POST',
            data: formData,
            contentType: false,
            cache: false,
            processData:false,
            dataType: "json",
            success: function(result) {
                // Success Code
            },
            error: function(result) {
                // Failure Code
            },
            timeout: 5000,
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

To post data with jQuery and Ajax, you can do something like this

var myObj = {
    "email" : "[email protected]",
    "mobile" : "9898989898",
    "data" : [
        {
            "name":"xyz",
            "age":45
        },
        {
            "name":"xyz",
            "age":45
        }
    ]
};

$.post( "test.php", myObj)
.done(function( data ) {
    alert( "Data Loaded: " + data );
  });

That will post the data and alert the response of the request.

Comments

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.