简体   繁体   中英

action.php for a website send message form

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="Jouwemail@email.nl" 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 = 'zorg@ontzorg-zwolle.nl';

    $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 = "zorg@ontzorg-zwolle.nl";

  $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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM