Transfer an Array Value into a Function
Posted: Wed May 05, 2010 1:32 pm
Greetings,
Please help me to improve my script. Basically my following script works if I hard-coded the email (employee1@test.com) into the variable $to of function processingForm() . However I would like to use the URL parameter (http://www.mywebsite.com/email.php?employee=1) which is passed into an array value $email and then transfer into the variable $to of function processingForm(). In other words, if URL parameter ?employee=1 then $to="employee1@test.com; if ?employee=2 then $to="employee2@test.com" and so on:
Please help me to improve my script. Basically my following script works if I hard-coded the email (employee1@test.com) into the variable $to of function processingForm() . However I would like to use the URL parameter (http://www.mywebsite.com/email.php?employee=1) which is passed into an array value $email and then transfer into the variable $to of function processingForm(). In other words, if URL parameter ?employee=1 then $to="employee1@test.com; if ?employee=2 then $to="employee2@test.com" and so on:
Code: Select all
<?php
// Create an array
$BROemployee = array
(
"1" => array("Aemail" => "employee1@test.com"),
"2" => array("Aemail" => "employee2@test.com"),
"3" => array("Aemail" => "employee3@test.com"),
);
$employee = $_GET['employee']; //get URL parameter employee
$email = $BROemployee[$employee]["Aemail"]; //define variable $email
//
//Select the function to call
//
if (!isset($_REQUEST['submit'])) {
generatingForm();
} else {
processingForm();
}
//Get the sender's input from the form
function generatingForm() {
print("<html><head><title>Contact Web Form</title></head><form action=email.php method=post>");
print("<body><font face=Arial size=2>");
print("<table bgcolor=#ffffcc align=center><tr><td colspan=2><strong>Please contact us using this"
." form:</strong></td></tr>\n");
print("<tr><td><font color=red>*</font>Your Name:</td>"
."<td><input type=text size=25 name=fName></td></tr>\n");
print("<tr><td><font color=red>*</font>Your Email:</td>"
."<td><input type=text size=25 name=fEmail></td></tr>\n");
print("<tr><td>Your Company:</td>"
."<td><input type=text size=25 name=fCompany></td></tr>\n");
print("<tr><td>Your Phone:</td>"
."<td><input type=text size=25 name=fPhone></td></tr>\n");
print("<tr><td colspan=2>Message:</td></tr>");
print("<tr><td colspan=2 align=center><textarea name=fMessage rows=15 cols=35></textarea></td></tr>");
print("<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr>");
print("<tr><td colspan=2>"
.'<input type=submit name=submit value="Send Mail">'
."<td></tr></table>\n");
print("</form></body></html>\n");
}
//
// Send the email
function processingForm() {
$to = "employee1@test.com";
$from = $_REQUEST['fEmail'] ;
$name = $_REQUEST['fName'] ;
$headers = "From: $from";
$subject = "Web Contact Form";
// Create an array for the inputs
$fields = array();
$fields{"fName"} = "Name";
$fields{"fCompany"} = "Company";
$fields{"fEmail"} = "Email";
$fields{"fPhone"} = "Phone";
$fields{"fMessage"} = "Message";
//
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// Send email to sender to confirm the action
$headers2 = "From: webmaster@test.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";
// Verify that the sender has entered the required his/her email and name
if($from == '') {print "Sorry! You have not entered your email, please go back and try again";}
else {
if($name == '') {print "Sorry! You have not entered your name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.mywebsite.com" );}
else
{print "We encountered an error sending your mail, please notify webmaster@test.com"; }
}
}
}
?>