Send more data than only the form (node.js post)2019 Community Moderator ElectionJavaScript post request like a form submitTrigger a button click with JavaScript on the Enter key in a text boxHow can I upload files asynchronously?Obtain form input fields using jQuery?How can I convert a string to boolean in JavaScript?Convert form data to JavaScript object with jQueryjQuery AJAX submit formapplication/x-www-form-urlencoded or multipart/form-data?jQuery Ajax POST example with PHPHow is an HTTP POST request made in node.js?
Vocabulary for giving just numbers, not a full answer
What is the magic ball of every day?
Bash script should only kill those instances of another script's that it has launched
In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?
Single word request: Harming the benefactor
Hotkey (or other quick way) to insert a keyframe for only one component of a vector-valued property?
How to write ı (i without dot) character in pgf-pie
Should I take out a loan for a friend to invest on my behalf?
Plausibility of Mushroom Buildings
Does a warlock using the Darkness/Devil's Sight combo still have advantage on ranged attacks against a target outside the Darkness?
How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?
Why would one plane in this picture not have gear down yet?
Why does Captain Marvel assume the people on this planet know this?
Filtering SOQL results with optional conditionals
How does NOW work?
Reversed Sudoku
weren't playing vs didn't play
Does this video of collapsing warehouse shelves show a real incident?
NASA's RS-25 Engines shut down time
Is "history" a male-biased word ("his+story")?
How does one describe somebody who is bi-racial?
Find longest word in a string: are any of these algorithms good?
They call me Inspector Morse
Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?
Send more data than only the form (node.js post)
2019 Community Moderator ElectionJavaScript post request like a form submitTrigger a button click with JavaScript on the Enter key in a text boxHow can I upload files asynchronously?Obtain form input fields using jQuery?How can I convert a string to boolean in JavaScript?Convert form data to JavaScript object with jQueryjQuery AJAX submit formapplication/x-www-form-urlencoded or multipart/form-data?jQuery Ajax POST example with PHPHow is an HTTP POST request made in node.js?
Is it possible when someone submits a form it sends additional things instead of only the input fields? In my case it would be the username.
javascript node.js forms post webforms
add a comment |
Is it possible when someone submits a form it sends additional things instead of only the input fields? In my case it would be the username.
javascript node.js forms post webforms
add a comment |
Is it possible when someone submits a form it sends additional things instead of only the input fields? In my case it would be the username.
javascript node.js forms post webforms
Is it possible when someone submits a form it sends additional things instead of only the input fields? In my case it would be the username.
javascript node.js forms post webforms
javascript node.js forms post webforms
asked Mar 7 at 5:57
SW30000SW30000
52
52
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
As @jayarjo as said, you can intercept the form submission and handle it manually, for example you can use Ajax as follows.
Note:; To use Ajax make sure you add its script for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event)
event.preventDefault();
var data =
username: $('#username').val(),
;
$.ajax(
url: '/upload',
data: data,
method: 'POST'
).then(function (response)
$('body').append(response);
).catch(function (err)
console.error(err);
);
);
</script>
Hope this helps.
add a comment |
You can always intercept the submission of the form and handle it manually. In fact that's what you should do anyway if you want to validate the form fields beforehand.
The logic is pretty straightforward. You attach an onsubmit
event listener to the form. Prevent the auto submission by calling preventDefault()
on the event. Then collect the values from all the fields, do the validation (if email is in the proper format, if passwords match, etc.), add arbitrary accompanying properties to the object and post the whole thing to the server via ajax post request.
Since you are asking that kind of question I will recommend to use some JS library to make it easier, jQuery
is perfect for such case:
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
Of course /handle.php
is a fake endpoint so submission here obviously won't work.
How does this code look like?
– SW30000
Mar 7 at 6:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036982%2fsend-more-data-than-only-the-form-node-js-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @jayarjo as said, you can intercept the form submission and handle it manually, for example you can use Ajax as follows.
Note:; To use Ajax make sure you add its script for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event)
event.preventDefault();
var data =
username: $('#username').val(),
;
$.ajax(
url: '/upload',
data: data,
method: 'POST'
).then(function (response)
$('body').append(response);
).catch(function (err)
console.error(err);
);
);
</script>
Hope this helps.
add a comment |
As @jayarjo as said, you can intercept the form submission and handle it manually, for example you can use Ajax as follows.
Note:; To use Ajax make sure you add its script for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event)
event.preventDefault();
var data =
username: $('#username').val(),
;
$.ajax(
url: '/upload',
data: data,
method: 'POST'
).then(function (response)
$('body').append(response);
).catch(function (err)
console.error(err);
);
);
</script>
Hope this helps.
add a comment |
As @jayarjo as said, you can intercept the form submission and handle it manually, for example you can use Ajax as follows.
Note:; To use Ajax make sure you add its script for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event)
event.preventDefault();
var data =
username: $('#username').val(),
;
$.ajax(
url: '/upload',
data: data,
method: 'POST'
).then(function (response)
$('body').append(response);
).catch(function (err)
console.error(err);
);
);
</script>
Hope this helps.
As @jayarjo as said, you can intercept the form submission and handle it manually, for example you can use Ajax as follows.
Note:; To use Ajax make sure you add its script for example
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('#newform').on('submit', function (event)
event.preventDefault();
var data =
username: $('#username').val(),
;
$.ajax(
url: '/upload',
data: data,
method: 'POST'
).then(function (response)
$('body').append(response);
).catch(function (err)
console.error(err);
);
);
</script>
Hope this helps.
answered Mar 7 at 6:12
Ike MawiraIke Mawira
4818
4818
add a comment |
add a comment |
You can always intercept the submission of the form and handle it manually. In fact that's what you should do anyway if you want to validate the form fields beforehand.
The logic is pretty straightforward. You attach an onsubmit
event listener to the form. Prevent the auto submission by calling preventDefault()
on the event. Then collect the values from all the fields, do the validation (if email is in the proper format, if passwords match, etc.), add arbitrary accompanying properties to the object and post the whole thing to the server via ajax post request.
Since you are asking that kind of question I will recommend to use some JS library to make it easier, jQuery
is perfect for such case:
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
Of course /handle.php
is a fake endpoint so submission here obviously won't work.
How does this code look like?
– SW30000
Mar 7 at 6:00
add a comment |
You can always intercept the submission of the form and handle it manually. In fact that's what you should do anyway if you want to validate the form fields beforehand.
The logic is pretty straightforward. You attach an onsubmit
event listener to the form. Prevent the auto submission by calling preventDefault()
on the event. Then collect the values from all the fields, do the validation (if email is in the proper format, if passwords match, etc.), add arbitrary accompanying properties to the object and post the whole thing to the server via ajax post request.
Since you are asking that kind of question I will recommend to use some JS library to make it easier, jQuery
is perfect for such case:
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
Of course /handle.php
is a fake endpoint so submission here obviously won't work.
How does this code look like?
– SW30000
Mar 7 at 6:00
add a comment |
You can always intercept the submission of the form and handle it manually. In fact that's what you should do anyway if you want to validate the form fields beforehand.
The logic is pretty straightforward. You attach an onsubmit
event listener to the form. Prevent the auto submission by calling preventDefault()
on the event. Then collect the values from all the fields, do the validation (if email is in the proper format, if passwords match, etc.), add arbitrary accompanying properties to the object and post the whole thing to the server via ajax post request.
Since you are asking that kind of question I will recommend to use some JS library to make it easier, jQuery
is perfect for such case:
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
Of course /handle.php
is a fake endpoint so submission here obviously won't work.
You can always intercept the submission of the form and handle it manually. In fact that's what you should do anyway if you want to validate the form fields beforehand.
The logic is pretty straightforward. You attach an onsubmit
event listener to the form. Prevent the auto submission by calling preventDefault()
on the event. Then collect the values from all the fields, do the validation (if email is in the proper format, if passwords match, etc.), add arbitrary accompanying properties to the object and post the whole thing to the server via ajax post request.
Since you are asking that kind of question I will recommend to use some JS library to make it easier, jQuery
is perfect for such case:
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
Of course /handle.php
is a fake endpoint so submission here obviously won't work.
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
$('#myForm').on('submit', function(e)
e.preventDefault();
const $form = $(this);
const method = $form.attr('method');
const action = $form.attr('action')
const fields = $form.serializeArray();
fields.push(
name: 'username',
value: 'myUsername'
)
const queryStr = $.param(fields);
$[method](action, queryStr)
.done(function()
// submission was successful - do something, refresh page for exmaple
)
.fail(function()
// submission failed
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myForm" method="post" action="/handle.php">
My name is: <input type="text" name="fname" /><br />
<label for="agree">I agree</label> <input type="checkbox" name="agree" value="1" /><br />
<button type="submit">Submit</button>
</form>
edited Mar 7 at 6:21
answered Mar 7 at 5:59
jayarjojayarjo
6,381136397
6,381136397
How does this code look like?
– SW30000
Mar 7 at 6:00
add a comment |
How does this code look like?
– SW30000
Mar 7 at 6:00
How does this code look like?
– SW30000
Mar 7 at 6:00
How does this code look like?
– SW30000
Mar 7 at 6:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036982%2fsend-more-data-than-only-the-form-node-js-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown