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)
Of course there is no "action" or "php" in the fetch() documentation...
elgonzo‭ wrote about 3 years ago · edited about 3 years ago

fetch() is chiefly about making requests, and is not about "actions" or "php". So why would you expect fetch()'s documentation to say something about random things that are not about requests themselves?

And how you should realize your fetch() or submit() depends entirely on how you have defined your backend endpoint with respect to request method and the data expected/required to be in the request.

It doesn't matter in what programming language you have implemented your backend endpoint. It only matters what request method that endpoint responds to (per your question, it seems to be POST), and what information in whatever data structure it expects/requires. You saying that your backend is written in PHP is not helpful, because it doesn't convey any information about which request mehthod your endpoint responds to, nor does it convey anything about what data in whichever shape and form it requires to be in the request...

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

If you are looking for examples of how to make requests with the fetch() method (including POST requests), you might want to take a look here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

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

elgonzo‭ thanks,

I misunderstand something --- if I don't specify a specific backend file (as I would do with the action attribute) how could submit() or fetch() "know" which backend directives (from all those available in the server) to use?

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

deleted user, the endpoint of a request is given by a URL, not a file. If the URL itself specifies some file name, then it's just part of that URL. The correct URL for a request (and what data and in what shape that data has to be included in the request) depends on how you have the backend implemented and configured, which you have said nothing about aside from "php!"

Look at the examples of fetch requests i linked to in my former comment. Note how a call to fetch() has to include the URL of the endpoint that will receive the request.

action is describing an aspect of a form. A form is not the same as a request. Submitting a form might initiate a request (or it might not). But that doesn't change the fact that action describes something about a form, it is not a property/attribute/whatever of a request. Hence why you would not find anything about action in the doc for fetch(). Because fetch() is about dealing with requests. fetch() is not about dealing with forms.

deleted user wrote about 3 years ago

elgonzo‭ The PHP file with backend directives (email address and such) is not the same PHP file containing the form's HTML (perhaps you meant to say that they should be the same?).

I hope you will excuse me if I would try to edit the question to not have an XY problem.

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

deleted user

(perhaps you meant to say that they should be the same?).

No, i did not say this, and i did not mean to say this either. I just said that you have to use the correct URL, whatever the actual correct URL is. (I am not going to wildly speculate about what the correct URL might be. You are in the position to know the correct URL for the request, as you are the one writing and running the backend.)

deleted user wrote about 3 years ago

I tried to redefine the problem and ask a better question.