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" : "xyz@gmail.com",
    "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?

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46

2 Answers2

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,
        });
    });
});
Ravi Shankar
  • 1,559
  • 1
  • 11
  • 15
0

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

var myObj = {
    "email" : "xyz@gmail.com",
    "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.

Computer's Guy
  • 5,122
  • 8
  • 54
  • 74