9 examples of 'javascript postform' in JavaScript

Every line of 'javascript postform' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
45function dopost(form) {
46 localStorage.name = form.name.value.replace(/ ##.+$/, '');
47 if(form.email.value != 'sage')
48 localStorage.email = form.email.value;
49
50 saved[document.location] = form.body.value;
51 sessionStorage.body = JSON.stringify(saved);
52
53 return form.body.value != "" || form.file.value != "";
54}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

33function postForm(event) {
34 // Retrieve the Form and submit button elements
35 var form = jQuery("#form");
36 var submit = jQuery(event.currentTarget);
37
38 // The server URL can be retrieved from the Form 'action' event
39 var url = form.attr('action');
40
41 // Use jQuery serialize function to serialize the Form controls into key/value pairs
42 // Note: the jQuery serialize function will *not* add the button name/value
43 // that submitted the form. We will add the submit button name/value manually
44 var formData = form.serialize();
45
46 // Append the form ID attribute so that Click can identify the Ajax target Control
47 formData+='&'+form.attr('id')+'=1';
48
49 // Append the name/value pair of the Submit button that submitted the Form
50 formData+='&'+submit.attr('name')+'='+submit.attr('value');
51
52 jQuery.post(url, formData, function(data) {
53 // Update the target div with the server response and style the div by adding a CSS class
54 var div = jQuery('#target');
55 div.addClass('infoMsg');
56 div.html(data);
57 });
58}
11postForm( params, newTab = false ) {
12 const form = jQuery( '<form method=\'POST\' style=\'display:none;\'></form>' ).appendTo(
13 document.body
14 );
15
16 if ( newTab ) {
17 form.attr( 'target', '_blank' );
18 }
19
20 for ( const i in params ) {
21 if ( params.hasOwnProperty( i ) ) {
22 $( '<input type="hidden" />' )
23 .attr( {
24 name: i,
25 value: params[i]
26 } )
27 .appendTo( form );
28 }
29 }
30
31 form.submit();
32 form.remove();
33}
61function postForm($form, callback) {
62 var values = {};
63 $.each($form.serializeArray(), function(i, field) {
64 values[field.name] = field.value;
65 });
66
67 $.post($form.attr('action'), values)
68 .done(function(data) {
69 callback(data);
70 })
71 .fail(function() {
72 generalError;
73 });
74}
21function post_request() {
22
23 var params = JSON.stringify({
24 request: "registerUser",
25 token: "1",
26 user:{
27 username: document.getElementById("usernameInput").value,
28 email: document.getElementById("emailInput").value,
29 password: document.getElementById("passwordInput").value
30 }
31 });
32
33 $.ajax( {
34 type: "POST",
35 url: "$(API_URL)",
36 headers: { 'Access-Control-Allow-Origin': document.domain },
37 data: params,
38 contentType: "application/json",
39 dataType: "json",
40 jsonp: false,
41 xhrFields: {
42 withCredentials: true
43 },
44 success: function() {
45 alert("Success!");
46 },
47 error: function(xhr, status, error) {
48 var err = eval("(" + xhr.responseText + ")");
49 alert(err.Message);
50 }
51 })
52 }
19function submitFormPost() {
20 var type = $('type').value;
21
22 if (type == 'image' || type == 'audio') {
23 return $('formPost').submit();
24 }
25
26 form = new ValidateForm($('formPost'),"admin/post/verify");
27 form.errorElem = $('error_messages');
28 form.successCallback = function() {callbackPostSuccess();};
29 return form.submit();
30}
1export default async function postForm(form: HTMLFormElement): Promise<Response> {
2 // `content.fetch` is Firefox’s way to make fetches from the page instead of from a different context
3 // This will set the correct `origin` header without having to use XMLHttpRequest
4 // https://stackoverflow.com/questions/47356375/firefox-fetch-api-how-to-omit-the-origin-header-in-the-request
5 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#XHR_and_Fetch
6 const contentFetch = window.content?.fetch ?? window.fetch;
7
8 const response = await contentFetch(form.action, {
9 // TODO: drop `as` after https://github.com/microsoft/TSJS-lib-generator/issues/741
10 body: new URLSearchParams(new FormData(form) as URLSearchParams),
11 method: 'POST',
12 headers: {
13 'Content-Type': 'application/x-www-form-urlencoded'
14 }
15 });
16
17 if (!response.ok) {
18 throw new Error(response.statusText);
19 }
20
21 return response;
22}
2function formPostData(url, fields) {
3 var form = $('<form style="display: none;" method="post" action="' + url + '"></form>');
4 $.each(fields, function(name, value) {
5 var input = $('<input type="hidden" name="' + name + '">');
6 input.attr('value', value);
7 form.append(input);
8 });
9
10 $(document).find('body').append(form);
11
12 form[0].submit(function(e) {
13 e.preventDefault();
14 });
15
16 form.remove();
17}
309function post(path, params) {
310 // create a form and set its attributes
311 var form = document.createElement("form");
312 form.setAttribute("method", "post");
313 form.setAttribute("action", path);
314
315 // set the attribute for the post
316 for(var key in params) {
317 if(params.hasOwnProperty(key)) {
318 var hiddenField = document.createElement("input");
319 hiddenField.setAttribute("type", "hidden");
320 hiddenField.setAttribute("name", key);
321 hiddenField.setAttribute("value", params[key]);
322
323 form.appendChild(hiddenField);
324 }
325 }
326
327 // submit the post
328 document.body.appendChild(form);
329 form.submit();
330}

Related snippets