【发布时间】:2014-04-21 22:20:35
【问题描述】:
所以我一直在研究 PHP 以创建一个基本的电子邮件表单,我遇到了一些代码,我决定看看并尝试一下。
我正在使用的 HTML 表单是:
<form method="post" action="newsjoin.php">
<table align=center>
<tr>
<td><select name="sendto" hidden="hidden"> <option value="newsletter@myDomain.com" selected="selected">Newsletter</option> </select></td>
</tr>
<tr>
<td><font color=red>*</font> Name:</td>
<td><input size=25 name="Name"></td>
</tr>
<tr>
<td><font color=red>*</font> Email:</td>
<td><input size=25 name="Email"></td>
</tr>
<tr>
<td><input type="radio" name="list" value="No" hidden="hidden" /> <input type="radio" name="list" value="Yes" hidden="hidden" checked="checked" /></td>
</tr>
<tr>
<td colspan=2 align=left><input type=submit name="send" value="Submit"></td>
</tr>
<tr>
<td><br /></td>
</tr>
<tr>
<td colspan=2 align=left><small>A <font color=red>*</font> indicates a field is required</small></td>
</tr>
</table>
</form>
我的 newsjoin.php 文件包含:
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"list"} = "Mailing List";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: newsletter@myDomain.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for subscribing to our mailing list. You should recieve another email shortly confirming our reciept of this information.";
if($from == '') {
print "You have not entered an email, please go back and try again.";
}
else {
if($name == '') {
print "You have not entered a name, please go back and try again.";
}
else {
$send = mail($to, $subject, $body, $headers); // this is the mail to site staff
$send2 = mail($from, $subject2, $autoreply, $headers2); // this is the mail to the user
if($send) {
header( "Location: http://www.myDomain.com/newsletter_joined.html" );
}
else {
print "We encountered an error sending your mail, please notify admin@myDomain.com";
}
}
}
?>
现在,这一切对我来说看起来都不错,只是我不明白这一行:
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
也就是说,我不了解$a => $b 部分,也不了解这些变量(如果它们是变量)在sprint() 函数中的使用。
在过去的 4 个小时里,我一直在 Google 上尝试了解我可以如何了解如何以这种方式使用 php,但没有运气。
谢谢!
【问题讨论】:
-
花了这四个小时,请您概述一下您的问题在哪里可以理解?是什么让你无法理解?
标签: php