简体   繁体   English

php email 从 html 表格

[英]php email from html form

So I've had some issues trying to get my php code to work.所以我在尝试让我的 php 代码工作时遇到了一些问题。 I found a form php handler which I want to send the email on the back end for the users.我找到了一个表格 php 处理程序,我想在后端为用户发送 email。

 <?php

if(isset($_POST['submit'])){
    ob_start();
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];//Sender first name//
    $last_name = $_POST['last_name'];//sender last name//
    $subject = "Contact Request.";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $to = "-myemail@outlook.com-"; // this is your Email address

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    header('Location: http://-mysite-/thankyou.html');
    exit();
}
?>

Here is the HTML form info:这是 HTML 表格信息:

<form action="php/email.php" method="post" name = "email form" enctype="multipart/form-data" class="container" align="center">
    <label for="firstname"></label>
    <strong>*</strong><input type="text" placeholder="Enter First Name" name="first_name" required>
    <br>
    <label for="lastname"></label>
    <strong>*</strong><input type="text" placeholder="Enter Last Name" name="last_name" required>
    <br>
    <label for="email"></label>
    <strong>*</strong><input type="text" placeholder="Enter Email" name="email" required>
    <br>
    <label for="phoneno"></label>
    <input type="phoneno" placeholder="Enter Phone Number" name="phone_number">
    <br>
    <label for="interested"></label>
    <t> Brief description of project:</t><br>
    <textarea style="width:100% height:60%" align="center" name="message">
    </textarea><br>
    <button type="submit" class="btn" value="Send Form"><strong>Submit</strong></button>
  </form>

I can't figure out why the page on submit does redirect to the email.php but doesn't do anything from there.我不知道为什么提交页面确实重定向到 email.php 但从那里没有做任何事情。 Do I need to contact the site support team to restart my php services?我是否需要联系站点支持团队来重新启动我的 php 服务? I've been smacking my head against the keyboard for the last 2 days as to why this isn't working.在过去的两天里,我一直在敲击键盘,为什么这不起作用。

Because you haven't field in your form with name="submit" and trying to get its value in PHP: if(isset($_POST['submit'])) .因为您的表单中没有name="submit"字段并试图在 PHP: if(isset($_POST['submit']))中获取其值。

Bonus tip: don't use name submit for any field of the form, cause some browsers getting fooled with this.额外提示:不要对表单的任何字段使用名称submit ,这会导致一些浏览器被愚弄。 Instead use formsubmit , submitted or anything else.而是使用formsubmitsubmitted或其他任何东西。

just add this field in your form只需在表单中添加此字段

<input type="hidden" name="submitted" value="1">

andchange condition in PHP to并将 PHP 中的条件更改为

if(isset($_POST['submitted']) && intval($_POST['submitted']) == 1)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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