Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Submitting a form via XHR/AJAX causes partial data arrival to email inbox (only HTML without input)

I am guessing a little here. By not preventingdefault, the form will POST the data to the server. If you switch to AJAX you have to provide the body as per documentation. However, I do not rememb...

posted 3y ago by Alexei‭  ·  edited 3y ago by Alexei‭

Answer
#2: Post edited by user avatar Alexei‭ · 2021-03-29T15:22:24Z (about 3 years ago)
added the jQuery way
  • I am guessing a little here. By not preventingdefault, the form will POST the data to the server.
  • If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send).
  • However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791):
  • const url = "http://example.com";
  • fetch(url, {
  • method : "POST",
  • body: new FormData(document.getElementById("contact_form")),
  • }).then(
  • response => response.text() // .json(), etc.
  • ).then(
  • html => console.log(html)
  • );
  • I am guessing a little here. By not preventingdefault, the form will POST the data to the server.
  • If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send).
  • However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791):
  • const url = "http://example.com";
  • fetch(url, {
  • method : "POST",
  • body: new FormData(document.getElementById("contact_form")),
  • }).then(
  • response => response.text() // .json(), etc.
  • ).then(
  • html => console.log(html)
  • );
  • A jquery way of achieving a similar HTTP call is provided by [this answer](https://stackoverflow.com/a/5772905/2780791):
  • $(document).ready(function(){
  • $("#frm").submit( function () {
  • $.post(
  • 'script.php',
  • $(this).serialize(),
  • function(data){
  • // do something with data
  • }
  • );
  • return false;
  • });
  • });
#1: Initial revision by user avatar Alexei‭ · 2021-03-29T15:07:49Z (about 3 years ago)
I am guessing a little here. By not preventingdefault, the form will POST the data to the server.

If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send).

However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791):

	const url = "http://example.com";
	fetch(url, {
		method : "POST",
		body: new FormData(document.getElementById("contact_form")),
	}).then(
		response => response.text() // .json(), etc.
	).then(
		html => console.log(html)
	);