1
  1. get Form with id
  2. create formdata with Form ID
  3. send Data with $.ajax

My code:

<form id="company">
    <input type="text" name="name" />
    <input type="tel" name="tel" />
    <input type="button" id="send" name="send" value="send" />
</form>


 <script>
        ##get Form id an create form data##
        var testForm = document.getElementById('send');
        testForm.onclick = function(event) {
            var formData = new FormData(document.getElementById('company'));
        ##this is Ajax Method##
                $.ajax({
                    url : './json/company.php',
                    method : 'POST',
                    data : formData,
                    timeout : 10,
                    dataType :'json',
                    success: function(data)
                    {
                        alert("Success");
                    }
                });
        }
    </script>

Error : TypeError: Argument 1 of FormData.constructor is not an object

Jaap
  • 81,064
  • 34
  • 182
  • 193
ali
  • 31
  • 7

4 Answers4

0

Try this

data : JSON.stringify(formData)
Aleksandar Matic
  • 789
  • 1
  • 9
  • 21
0

Since your form doesn't have a input type file you should use the serialize function instead

$(testForm).click(function(event) {
                event.preventDefault();//don't forget to prevent the default click event from redirecting you from the page
                $.ajax({
                    url : './json/company.php',
                    method : 'POST',
                    data : $('#company').serialize(),
                    timeout : 10,
                    dataType :'json',
                    success: function(data)
                    {
                        alert("Success");
                    }
                });
        });
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

You're missing crucial options, and jQuery is trying to process the formData, which it can't.
You have to add the processData: false option, and you should avoid sending a contentType as well

var testForm = document.getElementById('send');
testForm.onclick = function(event) {
    var formData = new FormData(document.getElementById('company'));
    $.ajax({
        url: './json/company.php',
        method: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        timeout: 10,
        dataType: 'json',
        success: function(data) {
            alert("Success");
        }
    });
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

In order to use formdata with jquery you have to set the correct options

var testForm = document.getElementById('send');
testForm.onclick = function(event) {
    var formData = new FormData(document.getElementById('company'));
    $.ajax({
        url : './json/company.php',
        type: "POST",
        data : formData,
        processData: false,
        contentType: false,
        success: function(data)
        {
          alert("Success");
        },
        error: function(jqXHR, textStatus, errorThrown){
            //if fails     
        }
    });
}
Shiva Srikanth Thummidi
  • 2,898
  • 4
  • 27
  • 36