【问题标题】:PHP file name added to URL but not called from htmlPHP 文件名添加到 URL 但未从 html 调用
【发布时间】:2019-12-06 01:41:11
【问题描述】:

当使用 html 表单调用 php 将包含表单中详细信息的电子邮件发送到 php.ini 中指定的电子邮件地址时。 php 文件名添加到 URL,但没有发送电子邮件,页面显示为空白。

该网站使用 php 7.3 托管在 my.ionos.co.uk (1&1) 上。

<form method="post" action="contact-form.php">
    <input class="contact" type="text" name="Name" placeholder="Name">
    <input class="contact" type="text" name="Email" placeholder="Your Email Address">
    <input class="contact" type="text" name="Subject" placeholder="Subject">
    <textarea class="contact" name="Message" rows="10" placeholder="Message"></textarea>
    <input class="contact-submit" type="submit" value="Send Message">
</form> 
<?php

if(isset($_POST['submit'])) {
    $name = $_POST['Name'];
    $mailFrom = $_POST['Email'];
    $subject = $_POST['Subject'];
    $message = $_POST['Message'];

    $mailTo = "X@hotmail.com";
    $headers = "From: ".$mailFrom;
    $txt = "You have recieved an email from ".$name.".\n\n".$Message;

    if(mail($mailTo, $subject, $txt, $headers)){
        echo "<h1>Mail send successfully</h1>";
    } else {
        echo "<h1>Mail failed to send<h1>";
    }
    header("Location: contact.html?mailsend");
}

?>

如果有人可以提供帮助,我将不胜感激,在此先感谢!

【问题讨论】:

  • contact-form.php 中尝试添加var_dump($_POST);$_POST var 可以吗?

标签: php html


【解决方案1】:

尝试改变:

 if(isset($_POST['submit'])) {

 if(isset($_POST['Email'])) { //or another field in your form. If you want you can also add in your submit button: name="submit"

检查您的代码似乎没有名称为submit 的输入。所以你永远不会进入if 的情况。

【讨论】:

  • 谢谢!我在表单中添加了 name="submit"
  • 如果您解决了您的问题,请考虑接受答案,谢谢! meta.stackexchange.com/a/5235
猜你喜欢
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2015-01-15
  • 2012-07-03
相关资源
最近更新 更多