【发布时间】: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 标记之后,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,看看它是否产生任何结果。 -
再次添加错误报告。空白页表示语法错误。