I have created a simple backend-frontend (HTML-JavaScript-CSS-PHP) contact form to transfer contact form inquiries to my local email client (i.e. local in the sense that it sits on the same web hosting provider environment of my website shared hosting environment).
As can be understood from the code; after the form is submitted the user is supposed to stay on the same webpage (some kind of a “refresh webpage” action on the contact form’s URL).
What I desire to have is the user getting some AJAX/AJAJ/XHR/JHR alert() or popup/modal message like “Thanks! I should most likely get back to you as soon as possible!” between the form submission and the refreshing of the page.
When the title of your post is as long as your code…
The long and short of it is:
Add an event listener to the Submit event;
Have that Submit event stop the form’s default action.
Use AJAX to submit the form.
Throw an alert when the AJAX returns.
If desired, have Javascript reload the contact form page after the alert.
At no point does the browser ‘go to’ your PHP page, so the user doesnt see any change in their web experience.
With the following code, I can get an email in my inbox and in my browser I stay with the same webpage (i.e. there is no longer the redirection to the PHP blank file)
let contactForm = document.querySelector("#contact_form")
contactForm.addEventListener("submit", function(event){
event.preventDefault()
var xhr = new XMLHttpRequest();
xhr.open(contactForm.method, contactForm.action, true);
xhr.setRequestHeader('Accept', 'application/json; charset=utf-8');
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send();
});
But, the HTML structure sent to my inbox is empty — it doesn’t contain any of the data I filled in the form, so I get something like:
well you told it where to go, you told it what verb to use to communicate to the server, you told it what headers to give.
What you didnt do was give it any sort of payload to carry.
You made a very pretty box and put it in the mailbox, but you didnt put anything inside the box.
As far as I know there is no heredocument syntax in JavaScript but perhaps something like the following pseudocode inside xhr.SOME_METHOD(message_variable) can be a substitute?
What i’m saying is your javascript is not sending the form data to the PHP page.
There should be either something in the .send() parameters or another line between those two. I’m spitballing here, as I have to head out in a minute, but inline it’d be something along the lines of