简体   繁体   中英

Submit a form with an additional POST var (NO AJAX)

Is possible to submit a from (synchronous normal classic way, no AJAX) with an additional POST var?

Using AJAX is easy:

$("#cpa").submit(function(e) {
    e.preventDefault();
    newvalues = { name: "John", time: "2pm" }; 
    $.post(theurl, newvalues);
});

But i like to know if can SUBMIT (so the page reloads or goes to theurl) the form with additional data.

EDIT: Since there is a confusion on the comments, ill like to share more code:

http://jsfiddle.net/4zc4d/

Each time you click save the content of the alert is what i want to add to the form vars.

Use

<input type="hidden" name="country" value="Norway">

as seen in w3schools to submit fields that the user shall not see. Beware: If the user cares to check sourcecode or traffic, he can find out the values.

EDIT: About the additional fields in the jsfiddle: Did you realise you can send arrays directly?

 <input type="hidden" name="id[]" value="1">
 <input type="hidden" name="id[]" value="2">
 <input type="hidden" name="id[]" value="3">

will result in

$_POST['id'] = array(1,2,3)

No need to concat values.

You can also add the hidden input via jQuery if you want to:

$("#cpa").submit(function(e) {
    $(this).append('<input type="hidden" name="theName" value="some value" />');
});

As someone mentioned, add a hidden field to your form HTML

<input type="hidden" id="hidValue" />

Set the value required.

$("#hidValue").val("Anyvalue");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM