我想要做的是将提交的姓名、邮箱和消息发送到我的php脚本,然后发送邮箱消息.问题是我的表单操作没有触发,而是重新加载页面.

UPDATE个 我不知道我在这里的html表单中遗漏了什么:

<form method="post" action="">
    <div class="input-group">
        <input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
        <span id="invalid-name">
            Please enter at least 2 chars
        </span>
    </div>
    <div class="input-group">
        <input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
        <span id="invalid-email">
            Please enter valid email
        </span>
    </div>
    <div class="input-group">
        <textarea id="message" name="message" placeholder="Message">
        </textarea>
        <span id="invalid-message">
            Please write something for us
        </span>
    </div>
    <div>
    </div>
    <input type="submit" name="submit" value="Book a Demo">
</form>

UPDATE
PHP脚本首先获取值并构造邮箱消息,然后最后发送:

<?php
if (isset($_POST['submit'])) {
  include 'index.php';
  $to = "email@example.com"; // this is your Email address
  $from = $_POST['email']; // this is the sender's Email address
  $name = $_POST['name'];
  $subject = "Form submission";
  $subject2 = "Copy of your form submission";
  $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
  mail($to, $subject, $message, $headers);
  mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
  echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
  // You can also use header('Location: thank_you.php'); to redirect to another page.
} else {
  echo 'isset was false';
}
?>

这是我的文件夹 struct

enter image description here

我在本地主机ubuntu apache服务器上运行这个程序.

推荐答案

对于一件小事来说,您的HTML很好,在点击提交之后,您没有指定将表单发送到哪里.这是通过在<form>中的action参数内指定PHP脚本来实现的

<form method="post" action="form-to-email.php">
    <div class="input-group">
        <input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
        <span id="invalid-name">
            Please enter at least 2 chars
        </span>
    </div>
    <div class="input-group">
        <input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
        <span id="invalid-email">
            Please enter valid email
        </span>
    </div>
    <div class="input-group">
        <textarea id="message" name="message" placeholder="Message">
        </textarea>
        <span id="invalid-message">
            Please write something for us
        </span>
    </div>
    <div>
    </div>
    <input type="submit" name="submit" value="Book a Demo">
</form>

在阅读了别人的回答上的问题后,我终于找到了你想要的,你可以通过在脚本的末尾添加一个标题(Location:index.html)来阻止网站停留在你的phpscript页面上,这样当它完成注册或失败时,你可以将它发送回索引.或者,您可以在索引文件中包含PHP,但您必须将索引从.html更改为.php

在php脚本中使用重定向的示例

<?php
if (isset($_POST['submit'])) {
  include 'index.php';
  $to = "email@example.com"; // this is your Email address
  $from = $_POST['email']; // this is the sender's Email address
  $name = $_POST['name'];
  $subject = "Form submission";
  $subject2 = "Copy of your form submission";
  $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
  mail($to, $subject, $message, $headers);
  mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
  echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
  header('Location: index.html');
} else {
  echo 'isset was false';
  header('Location: index.html');
}
?>

Php相关问答推荐

在WooCommerce中处理客户总支出中的退款

在PHP中替换数组中的文本并同时使用其他函数

使用.htaccess将任何路由重定向到文件

WooCommerce在store 页面上显示兼容多语言的产品属性

如何限制WordPress自定义分类术语页面中的术语数量

在PHP中使用curl命令执行SCRAPYD操作.json不返回任何内容

PHP文件上载问题-";Move_Uploaded_Files";未按预期工作

除WooCommerce中的特定国家/地区外,有条件地要求填写邮政编码 checkout 字段

未检测到laravel控制器

PHP 数组中第一级 node 到 XML 的唯一名称

将产品数量、名称和结帐链接添加到 WooCommerce 添加到购物车消息

PHP 中的 libgd 无法读取 Apache 配置中的 SetEnv GDFONTPATH

如何使用不同的方法编写嵌套连接查询并将两列连接成一列在 laravel 查询生成器中

如何在WooCommerce中为访客购买者分配自定义客户代码

在WooCommerce可变产品中显示所选变体的自定义字段

PHP 合并/合并多维数组

使用来自 PHP 表单的数据更新 xml 命名空间

PHP/Laravel 中的自定义时区

如何限制for循环php中的条目数

php-http/discovery 插件 1.15.1 是否仍在使用自己的模块 destruct Contao 4.13 中的composer 安装?