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.

Comments on How to submit form data with Ajax?

Post

How to submit form data with Ajax?

+1
−3

I need to apply backend behavior (PHP directives in my case) to an HTML <form>.

The backend behavior I wish to apply to the form is in this pattern (behavior.php):

<?php
	$name = $_POST['name'];   # Required;
	$email = $_POST['email']; # Required;
	$to = 'example@example.com';
	$subject = 'A new message from: ' . $name;
	$headers[] = 'MIME-Version: 1.0';
	$headers[] = 'Content-type: text/html; charset=utf-8';
	mail($to, $subject, $message, implode("\r\n", $headers));
?>

The way I know to do this is with this HTML pattern:

<form action="/behavior.php" method="POST">

Now I have a form that the default behavior of which is prevented with preventDefault() so clicking the submit button will not cause form submission.
Therefore the HTML action and method became ineffective and I need to send the form's data with Ajax but according to the backend behavior (under behavior.php) where the relevant email address is stored.

In MDN "Using Fetch" I didn't find any code example with such a file reference.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Not sure if that's what you need, but if I understood correctly, it'd be something like this: ```j... (10 comments)
Why do you have prevented the "default behavior" of the form? (3 comments)
Of course there is no "action" or "php" in the fetch() documentation... (7 comments)
Why do you have prevented the "default behavior" of the form?
elgonzo‭ wrote about 3 years ago · edited about 3 years ago

What exactly are your reasons to prevent the default behavior? Actually, what precisely is the behavior that you summarily describe as "default behavior" and that you desire to prevent?

Have you thought about not preventing that "default behavior" but just preventing the user from triggering the "default behavior", for example perhaps by not using or hiding the submit button, or something like that? This way it might still be suitable to trigger the desired action/behavior programmatically with the submit() method in JS... (Yes, i am now wildly speculating here ;-)

deleted user wrote about 3 years ago · edited about 3 years ago

elgonzo‭

I prevented the form's default behavior (which is all backend and moves the user to a standalone "Your message has been sent" webpage) with preventDefault(), so that a user could submit the form with Ajax and that then the form will be hidden by JavaScript (style.display = "none") and a "Your message has been sent" box will be displayed instead by JavaScript (style.display = "block").

I didn't quite understand the last passage.

elgonzo‭ wrote about 3 years ago

Ah, i see. I unfortunately cannot offer help with your problem. However, you might perhaps mention in your question that your frontend is using Ajax, and which thusly might encourage Ajax-based solutions that might be simpler/easier than plain/pure non-Ajax JavaScript (unless you seek a solution that should not depend on Ajax, of course).