0

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?

3
  • Typically you would have a index.php or html page in your top level folder on your server. There may be a pages folder, with css folder, and perhaps a javascript folder. Depending on where your action page is placed, you access it in child folder by using a /. 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" Commented May 17, 2020 at 7:39
  • And if you are going up one level in file level on your server, it would look like ../. So say I have a file name customers.php that lives in the second level of the server and there is a form and that form is posting to a file called user.php that lives in includes folder that is also second level in the server, I must leave the pages folder and go up one level to access the includes folder and subsequent user.php my action would look like: action="../includes/user.php" Commented May 17, 2020 at 7:51
  • Also, typically when handling form data via post, you should first see if the submit button that handles the form submission is set using isset(). if(isset($_POST['contact-submit'])){ //--> check and handle validate sanitize user input etc... } Commented May 17, 2020 at 7:53

1 Answer 1

1

You've done everything correctly, but you missed the name attribute in the input tags.

Your code should be like this,

<input class="c-form__field" id="contact-name" name="contact-name" type="text" placeholder="Bijvoorbeeld, Jade van de Ven" required>

You have to add the name attribute in all the input tags like above code...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.