Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据从HTML表单发送到php_Php_Html_Post - Fatal编程技术网

将数据从HTML表单发送到php

将数据从HTML表单发送到php,php,html,post,Php,Html,Post,我正在尝试将数据从HTML发送到PHP。以下是我的html代码: <div class="content"> <form action="email-script.php" method="post"> <div class="contact-form mar-top30"> <label> <span>Full name</span> <input type="te

我正在尝试将数据从HTML发送到PHP。以下是我的html代码:

<div class="content">
    <form action="email-script.php" method="post">
      <div class="contact-form mar-top30">
        <label> <span>Full name</span>
        <input type="text" class="input_text" name="name" id="name"/>
        </label>
        <label> <span>Email</span>
        <input type="text" class="input_text" name="email" id="email"/>
        </label>
        <label> <span>Subject</span>
        <input type="text" class="input_text" name="subject" id="subject"/>
        </label>
        <label> <span>Message</span>
        <textarea class="message" name="feedback" id="feedback"></textarea>
        </label>
        <label>
                <input type="submit" class="button" value="Send" />
        </label>
      </div>
    </form>
  </div>

全名
电子邮件
主题
消息
下面是我的php代码:

<!DOCTYPE html>
<html>
<?php
    $email=$_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["name"] . "Sent this email" . "\r\n" . $_POST["feedback"] . "Sent from this email address:" . $_POST["email"];
    mail("email@gmail.com", $subject, $message);
    //header("Location: index.html");
?>
Error, please contact Jack at (email@gmail.com) <br>
email: <?php $email ?> <br>
subject: <?php $subject ?> <br>
message: <?php $message ?> <br>
</html>

错误,请联系杰克(email@gmail.com)
电子邮件:
主题:
信息:
我想最好的方法是通过POST方法。如何通过单击submit按钮将所有数据从html表单发送到php电子邮件脚本。下面是该问题的截图

提交前:

提交后:

此外,您可以使用
$\u服务器['REQUEST\u METHOD']
检查是否通过post请求访问了
电子邮件脚本.php
,以避免直接访问文件时出现错误

只需用

if ($_SERVER['REQUEST_METHOD'] == "POST") {
     // validate the fields, send the email, etc.
}
你可以接受下一个请求


您还应该在发送电子邮件之前验证字段,这样就没有人可以向您发送空电子邮件。如果要添加更多安全性,请设置会话或使用谷歌验证码,这样就很难连续多次提交表单。

只需在输出变量之前添加echo即可

email: <?php echo $email ?> <br>
subject: <?php echo $subject ?> <br>
message: <?php echo $message ?> <br>
电子邮件:
主题:
信息:

变量不是空的,您只是忘记了
echo
它们会像这样更改您的代码:

<!DOCTYPE html>
<html>
<?php
    $email=$_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["name"] . "Sent this email" . "\r\n" . $_POST["feedback"] . "Sent from this email address:" . $_POST["email"];
    mail("email@gmail.com", $subject, $message);
    //header("Location: index.html");
?>
Error, please contact Jack at (email@gmail.com) <br>
email: <?php echo $email ?> <br>
subject: <?php echo  $subject ?> <br>
message: <?php echo  $message ?> <br>
</html>

错误,请联系杰克(email@gmail.com)
电子邮件:
主题:
信息:

单击“提交”按钮?是的,我希望“提交”按钮发送电子邮件。问题是在我的调试中,所有字段(电子邮件、消息等)都是空白的。哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈。但是我必须同意@Don'tpanic,你的php代码是用.php或.html编写的?首页是contact.php,第二个是it email-script.phpI计划,它会删除空白并清理我的数据。这只是建立的第一步。我试试这个。