Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add variable to FormData in jquery?

Actually i am using following script to post my form

var formData = new FormData($("form#driver_information")[0]);
$.ajax({
    type: "POST",
    url: "/",
    data: formData, 
    success: function(data) {
    $("#page_message_box").html(data);
    },
    cache: false,
    contentType: false,
    processData: false
});

I need to pass some more variables along with form data

eg:

var formData = new FormData($("form#driver_information")[0]);
 $.ajax({
  type: "POST",
  url: "/",
  data: formData + "&con=delete",  
  success: function(data) {
  $("#page_message_box").html(data);
 },
   cache: false,
   contentType: false,
   processData: false
});

But it's not working.( data: formData + "&con=delete", ). Please help to solve this problem.

like image 652
Dinesh G Avatar asked Nov 18 '15 09:11

Dinesh G


People also ask

How do I add data to FormData?

FormData.append() The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.

Can we send object in FormData?

Yes you can, you can append to formData objects.

What is FormData jquery?

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the fetch() or XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data" .


1 Answers

Try this:

formData.append('con', 'delete');

before the $.ajax call.

Then within that call you just need:

data: formData,
like image 147
rlarcombe Avatar answered Oct 04 '22 21:10

rlarcombe