Transfer an Array Value into a Function
Moderator: General Moderators
-
minorDemocritus
- Forum Commoner
- Posts: 96
- Joined: Thu Apr 01, 2010 7:28 pm
- Location: Chicagoland, IL, USA
Re: Transfer an Array Value into a Function
I can't reproduce your results... I thought it was a problem with passing string to the array brackets instead of int, but it works both ways for me. What version of PHP are you on? Please post your current code.
Re: Transfer an Array Value into a Function
Thanks minorDemocritus for continuing helping me. Our PHP version is 5.1.6. I have added echo to check the variables and it displayed $email = NULL before running the function processingForm($email).
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'])) {
echo "IF-BROemail=". $email."<br>";
generatingForm();
} else {
echo "ELSE-BROemail=". $email."<br>";
processingForm($email);
}
//Get the sender's input from the form
function generatingForm() {
print("<html><head><title>Contact Web Form</title></head><form action=forumemail.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");// Send the email
function processingForm($email) {
$to = $email;
$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"; }
}
}
}
?>