How to submit form data with Ajax?
I need to apply backend behavior (PHP directives in my case) to an HTML
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 = 'email@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
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.