我使用网上找到的表单在html网站上设置了一个表单。该表单工作得很漂亮,并向我发送了一封包含提交内容的电子邮件,因此我们知道它有效,服务器可以处理php和邮件等。
然后我更改了表单所在页面上的一些字段,并尝试编辑contact.php部分以匹配它,当然表单停止发送电子邮件。所以,我做错了什么但不知道究竟要在contact.php方面或contact.html方面做些什么改变才能让它再次运作。
此外,这种形式至少在某种程度上是安全的还是我还应该添加其他内容?这是我设立的第一个表格,我将永远感激一些帮助。
请注意:我确实在真实网站上使用了真实的电子邮件,而不是you@domain.com。另外,如果您将它指向假人,请。我和Methuselah一样年长,根本不理解这个PHP的东西,但我正在尝试学习。
原来的contact.php:
<?php
/* Set e-mail recipient */
$myemail = "you@domain.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
已停止发送邮件的已编辑contact.php:
<?php
/* Set e-mail recipient */
$myemail = "me@domain.com";
/* Check all form inputs using check_input function */
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname = check_input($_POST['lastname'], "Enter your last name");
$email = check_input($_POST['email'], "Enter your email");
$phone = check_input($_POST['phone'], "Enter your phone");
/*$how_time = check_input($_POST['time']);*/
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
First Name: $firstname
Last Name: $lastname
E-mail: $email
Best time to call: $how_time
End of message
";
/* Send the message using mail() function */
mail($myemail, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
html页面上的原始表单:
<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by <a href="http://myphpform.com">PHP form</a></p>
</form>
编辑过的html表单:
<form action="contact.php" method="post">
<p>
<table>
<tr class="label"><td>First Name</td><td>Last Name</td></tr>
<tr>
<td><input type="text" name="firstname" /></td>
<td><input type="text" name="lastname" /></td>
</tr>
<tr class="label"><td colspan="2">Email Address</td></tr>
<tr>
<td colspan="2" class="one-line">
<input type="text" name="email" /></td></tr>
<tr class="label"><td>Phone Number</td><td>Best time to call? (optional)</td></tr>
<tr>
<td>
<input type="text" name="phone" /></td>
<td>
<select name="time">
<option value="">- Please select -</option>
<option>Morning (8am - Noon)</option>
<option>Afternoon (Noon - 5pm)</option>
<option>Evening (After 5pm)</option>
</select>
</td>
</tr>
<tr class="last-row">
<td><span class="small"> </span></td>
<td><input type="submit" value="Get a Call Back"/></td>
</tr>
</table>
</form>
对不起,这是如此漫长而且参与其中,但我的斗智尽头。
注意:如果其他人遇到此问题,问题是两个项目而不是三个项目:
mail($myemail, $message);
我添加了“主题”并且它有效,即使表单本身没有主题字段。非常感谢那些建议的人 - 我现在找不到您的原始答案,感谢您的主题。我永远不会猜到它,这么简单,节省了很多时间和挫折。一个人不知道什么是不知道的。