So I've been researching into PHP to create a basic email form and I came across some code which I decided I would take a look at and experiment with.
The HTML form I am working with is:
<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>
And my newsjoin.php file contains:
<?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";
}
}
}
?>
Now, this all looks ok to me except for I don't understand this line:
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
Namely, I do not understand the $a => $b
section, nor do I understand the use of these variables(if they are variables) in the sprint()
function.
I have spent the past 4 hours on Google trying to learn what I can about php being used this way and had no luck.
Thanks!