【问题标题】:Php form issue with submitting [closed]提交时的PHP表单问题[关闭]
【发布时间】:2015-05-13 03:49:19
【问题描述】:

我一直在尝试制作一个 php 表单,以便用户可以发送电子邮件。但是由于我目前缺乏php知识,这有点难以解决。所以我收集了一些完成的sn-ps代码,但它仍然不起作用。

这是我在名为“submit.php”的文件中的 php 代码

<?php 
if(isset($_POST['submit'])){
    $to = "example@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_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 " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

这是我在其中包含 html 表单的“form.php”

<?php include('submit.php') ?>
    <div class="main-wrapper">
              <section class="contact-form">
        <form action="" method="POST">
         <h1>Contact</h1>
         <fieldset>

         <input maxlength="30" type="text" name="first_name" placeholder="John" required><br>

         <input maxlength="30" type="text" name="last_name" placeholder="Smith" required><br>

         <input maxlength="30" type="email" name="email" placeholder="john_doe@example.com" required>
          </fieldset>
         <fieldset>
         <textarea maxlength="300" placeholder="Write your text here...." id="message" name="message" cols="40" rows="6" required></textarea>
         <button name="submit" type="submit">Send Message</button>
          </fieldset>
        </form>
        </section>

还有人告诉我表单没有安全性, "因为它不会剥离 html 标签"

我该如何解决?

【问题讨论】:

  • “但还是不行。”——在什么方面不行?
  • 如果你想去除 html 标签,使用 htmlspecialchars()
  • 它不起作用,当我点击提交按钮时,没有发送电子邮件。
  • 将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,看看它是否产生任何结果。
  • 再次添加错误报告。空白页表示语法错误。

标签: php html forms


【解决方案1】:

您的表单未提交到 PHP 脚本

改变

<form action="" method="POST">

<form action="submit.php" method="POST">

编辑:我看到你包括submit.php,所以你可能想要这样做

<form action="form.php" method="POST">

【讨论】:

  • 当我将操作更改为 submit.php 并单击提交按钮时,我只会在显示 php 代码的白色页面上结束。
  • 如果action属性为空,则其值为当前URL。
  • @erdrag,如果你显示了 php 代码,那么你应该检查服务器是否支持 PHP。
  • 我该怎么做?它在我的虚拟主机网站上吗?
【解决方案2】:

您的服务器应该配置了 Sendmail 或不同的邮件服务器。

要剥离 HTML,您可以使用 $message = strip_tags($message); $message2 = strip_tags($message2);

或者您可以使用htmlspecialchars 将特殊字符转换为 HTML 实体。

【讨论】:

  • 这是一条评论。你怎么确定的?
  • 您是否阅读了他们所说的部分:“但它仍然不起作用。”
  • @Fred-ii-,很多时候我的朋友甚至我都尝试在未配置邮件服务器且无法发送邮件的不同服务器上使用mail()。这是主要原因:)
  • 如何配置邮件服务器?
  • @SpartakusMd 似乎这个问题还不清楚,OP 没有回答我的一个 cmets 在他们的问题下,关于他们如何/在哪里运行他们的脚本。
【解决方案3】:

这种语法已经为我工作了很多年:

"From: $from&lt;&gt;\n"; 中,&lt;&gt; 是名称通常所在的位置。留在那里,即使是空白。

$mailheaders = "From: $from<>\n";
$mailheaders .= "Reply-To: No1@noone.com\n";
mail($to,$subject, $message, $mailheaders, "-fmailer@yoursite.com");

【讨论】:

    【解决方案4】:

    此外,除了添加错误报告和服务器配置之外,您的标头还需要其他属性。否则,如果发送该电子邮件,最终将成为垃圾邮件。考虑添加以下头信息。

    /* well formed hearder */
    $headers = 'From: '.$senderemail."\r\n";
    $headers .= 'Reply-To: '.$senderemail."\r\n";
    $headers .= 'Return-Path: '.$senderemail."\r\n";
    /* additional header */
    $headers .= 'MIME-Version: 1.0'."\r\n";
    $headers .= 'Content-Type: text/plain;'."\r\n";
    $headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";
    

    最后,您可以在 $to 属性中使用逗号分隔的列表。因此,与其运行 mail() 两次,不如将你的地址连接起来。

    $to = "john_doe@example.com,example@example.com";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2015-10-08
      • 2020-06-08
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多