submitting form data on jquery button click

Viewed 141

I have a button by where onclick i am trying to send the form data to the server page to process. but somehow the formdata is going empty

can anyone guide what i am doing wrong here, here is my code

function ajax(obj) {
    alert('hello');
    console.log(obj);
    var form = document.querySelector('form');
    var data = new FormData(form);
    var post_url = $('#formid').attr("action"); //get form action url
    var form_data = $('#formid').serialize() & '&yes=1'; //Encode form elements for submission 
    console.log(form_data);
    console.log(data);
    $.post(post_url, form_data, function( response ) {
        $("#results").html(response);
    });
    return false;
}

the function ajax is called on the button click inside a form

1 Answers

You are using post menthod wich gets params in body serilliazing the args like that is good for GET request you should send in body just use a object holding all the info

$.ajax({
  type: "POST",
  url: url,
  data: data ,// object
  success: success,
  dataType: dataType
});
Related