如何创建 php 表单处理程序并与现有的 html Web 表单集成


How to create php form handler and intergrate with existing html web form?

这就是我面临的问题。我创建了一个非常简单的网络表单:

    <form  method="post"  action="#">
                  <div  class="field"> <label  for="name">Name</label>
                    <input  name="name"  id="name"  type="text"> </div>
                  <div  class="field"> <label  for="email">Email</label> <input
                       name="email"  id="email"  type="email"> </div>
                  <div  class="field"> <label  for="message">Message</label> <textarea
 name="message"  id="message"  rows="4"></textarea> </div>
                  <ul  class="actions">
                    <li><input  value="Send Message"  type="submit"></li>
                  </ul>
                </form>

我需要知道如何使用此表单将用户输入的数据发送到我的电子邮件地址 admin@nue-tech.uk 我知道这可以在 PHP 中完成,但不确定如何处理这个问题,因为我对 PHP 一无所知。如果有人能指出我正确的方向,说明如何做到这一点,以及我应该将PHP文件放在相对的位置,那就太棒了!

您可以复制下面的代码并将其粘贴到 html 下方的 HTML 文件中。在 html 中,您将操作属性设置为空。不要忘记将文件扩展名更改为.php

<?php 
if(isset($_POST['submit'])){
$to = "admin@nue-tech.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Blablabla"; //Write whatever you want here
$message = $name . "wrote the following:" . "'n'n" . $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page if the mail was send succesfully
} else {
header('location: contact.html'); //if it was not send succesfully it redirects to the contact page again
exit(0);
}
?>

header('location: contact.html');

您也可以使用

echo "Something went wrong. Try again later" 

或模拟的东西。我强烈建议您在W3Cschools或 php.net 上搜索和学习。