I have a form on a website to send a message, the message should be send to an email address.
As I am new to website building I am playing around but can't get the form working with a action.php file. When I have uploaded the HTML code and the action-page.php to the server (in the same subfolder) the message I type on the website is not send at all. I receive no email and when I click the send button and I am forwarded to a blank page in the browser. Preferably i just stay on the same page and get a "Your message has been sent" notification, but this is a "nice-to-have" I just want to get it working.
I used: https://html.form.guide/email-form/php-form-to-email.html as a reference for the code.
1. What is wrong or why it is not working?
The code for the website message form is the following:
<div class="o-screen-contact__col-form">
<form class="c-form js-form-validation" action="action-page.php" method="post">
<label for="contact-name">Jouw naam</label>
<input class="c-form__field" id="contact-name" type="text" placeholder="Bijvoorbeeld, Jade van de Ven" required>
<label for="contact-email">Jouw e-mail</label>
<input class="c-form__field" id="contact-email" type="email" placeholder="[email protected]" required>
<label for="contact-message">Jouw bericht</label>
<textarea class="c-form__field" id="contact-message" name="name" placeholder="Schrijf hier jouw bericht of zorg" required></textarea>
<fieldset class="u-text-right">
<button class="c-button c-button--primary" type="submit" name="contact-submit">Zend mij een bericht</button>
</fieldset>
</form>
</div>
I used the post method and made an action-page.php file for the actual message to be send to an email adress.
The action-page.php file looks like this:
<?php
$name = $_POST['contact-name'];
$visitor_email = $_POST['contact-email'];
$message = $_POST['contact-message'];
?>
<?php
$email_from = '[email protected]';
$email_subject = "Nieuwe email website ontzorg-zwolle";
$email_body = "You have received a new message from the user $contact-name.\n".
"Here is the message:\n $contact-message".
?>
<?php
$to = "[email protected]";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
2. Where to put the action-page.php file on the server
I have put the action-page.php file in the same folder als the index.html file (where the message form is in). Is that the right place? Or do I need to put the file in some other place to be called properly?
/. So if you have a file on the top level folder named index.php and its form is sending a post to action-page.php that is in the pages folder, the action attribute would look like;action="pages/action-page.php"../. So say I have a file namecustomers.phpthat lives in the second level of the server and there is a form and that form is posting to a file calleduser.phpthat lives inincludesfolder that is also second level in the server, I must leave thepagesfolder and go up one level to access theincludesfolder and subsequentuser.phpmy action would look like:action="../includes/user.php"isset().if(isset($_POST['contact-submit'])){ //--> check and handle validate sanitize user input etc... }