Christopher H
Christopher H

Reputation: 2194

Append POST data javascript

Using either Javascript and/or Jquery how can I append POST data to a form to submit. I have in my server side code that will check to see if the post data dictionary contains a certain key.

I already have a form in the code. So I would just like to use javascript to add a new key to the POST data.

Upvotes: 1

Views: 6026

Answers (3)

MCSI
MCSI

Reputation: 2894

quick example

you can add hidden inputs, for example:

var yourValue = 123;
$('form').append('<input type="hidden" id="yourData" name="yourData" value="'+ yourValue +'"/>');

so you get:

<input type="hidden" id="yourData" name="yourData" value="123"/>

and when you do the submit you are sending "yourData"

Upvotes: 3

Jarry
Jarry

Reputation: 1931

with jQuery:

var $input = $('<input>').attr(
{
    type: 'hidden',
    id: 'input_id',
    name: 'input_name',
    value: 'input_value'
}).appendTo('#form_name');

$('#form_name').submit();

is this what you were trying to do?

Upvotes: 3

Fabrizio
Fabrizio

Reputation: 3776

try to intercept the submit event and:

  • serialize the form, add what you need to add and use the $.post(url, data, function(){}); function.
  • inject the form with extra hidden input fields and then submit it via jquery

Upvotes: 1

Related Questions