Transfer an Array Value into a Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Transfer an Array Value into a Function

Post by wstran »

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:

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"; }
               }
               }
        }
?>
Last edited by wstran on Wed May 05, 2010 3:31 pm, edited 2 times in total.
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

Post by minorDemocritus »

Use [ syntax=php ] bbcode blocks to enclose your code. Otherwise most people won't bother reading it. I certainly won't.

And indent it too...
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

I applied the

Code: Select all

 and added the comments. Thanks!
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

Post by minorDemocritus »

Code: Select all

//Try changing this:
$email = $BROemployee[$employee]["Aemail"];

// To this:
$email = $BROemployee[strval($employee)]["Aemail"];

// And this: 
function processingForm() {
          $to = "employee1@test.com";

// To this:
function processingForm() {
          $to = $email;
Run the original script, and let me know what it does that's not expected.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Transfer an Array Value into a Function

Post by John Cartwright »

Functions have their own scope. Therefore, to access values outside of the function scope you need to pass the function the appropriate values. For instance, your processingForm() function would accept the email, i.e.,

Code: Select all

processingForm($BROemployee[$employee]["Aemail"]);

function processingForm($email) 
{
   $to = $email;

   // etc..
}
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

Post by minorDemocritus »

John Cartwright wrote:Functions have their own scope.
I should have caught that. RRRRGGGH :banghead:

Alternately, you could pull everything out of the functions, and replace the function calls with their respective blocks of code.
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

I edited my code as you recommended but still cannot send the email. Somehow the function did not get the array value.
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

Post by minorDemocritus »

wstran wrote:I edited my code as you recommended but still cannot send the email. Somehow the function did not get the array value.
You'll have to be more specific. Try posting the code you're using now.
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

Hello,
I just added what John and you recommended to my code but it's still not working. Please help thanks!

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($BROemployee[$employee]["Aemail"]);      
  }
//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($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"; }
               }
               }
        }
?>
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

Post by minorDemocritus »

Code: Select all

// This is wrong:
         $fields{"fName"} = "Name";
         $fields{"fCompany"} = "Company";
         $fields{"fEmail"} = "Email";
         $fields{"fPhone"} = "Phone";
         $fields{"fMessage"} = "Message";

// This is right:
         $fields["fName"] = "Name";
         $fields["fCompany"] = "Company";
         $fields["fEmail"] = "Email";
         $fields["fPhone"] = "Phone";
         $fields["fMessage"] = "Message";
Also, try just a simple email script like this, see if it works:

Code: Select all

<?php
// replace example@example.com with your email address
mail("example@example.com", "Testing", "This is a test of the PHP mail() function");
?>
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

Yes, the PHP mail works. Actually the line $send2 = mail($from, $subject2, $autoreply, $headers2) works for autoreplying. The only problem is the variable $to in function processingForm() does not get the employee's email from array. As John said it may not pass the value due to scope.
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

Post by minorDemocritus »

Try adding these lines to the location shown:

Code: Select all

$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
// EXTRA LINES START HERE
echo 'The var_dump of $BROemployee: ';
var_dump($BROemployee);
echo '<br /><br />The var_dump of $email: ';
var_dump($email);
echo '<br /><br />';
What do you get for the output of those lines?
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

The output is:

The var_dump of $BROemployee: array(3) { [1]=> array(1) { ["Aemail"]=> string(18) "employee1@test.com" } [2]=> array(1) { ["Aemail"]=> string(18) "employee2@test.com" } [3]=> array(1) { ["Aemail"]=> string(18) "employee3@test.com" } }

The var_dump of $email: NULL
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

Post by minorDemocritus »

OK, so the array is done correctly. How about this?

Code: Select all

$BROemployee = array
     (
           "1" => array("Aemail" => "employee1@test.com"),
           "2" => array("Aemail" => "employee2@test.com"),
           "3" => array("Aemail" => "employee3@test.com"),
     );
// ADD THIS IF STATEMENT
if (isset($_GET['employee'])) {
    $employee = $_GET['employee'];            //get URL parameter employee
} else {
    die('No employee found in URL parameter!');
}
$email = $BROemployee[$employee]["Aemail"];      //define variable $email
// EXTRA LINES START HERE
echo 'The var_dump of $employee: ';
var_dump($employee);
echo '<br /><br />';
What do you get for the output of that line? I have a feeling you're assuming $employee is a string type, but is actually being passed as an int, or the other way around.
wstran
Forum Newbie
Posts: 9
Joined: Wed May 05, 2010 12:07 pm

Re: Transfer an Array Value into a Function

Post by wstran »

Here is the output:

The var_dump of $employee: string(1) "1"

The var_dump of $BROemployee: array(3) { [1]=> array(2) { ["Aname"]=> string(9) "Employee1" ["Aemail"]=> string(18) "employee1@test.com" } [2]=> array(2) { ["Aname"]=> string(9) "Employee2" ["Aemail"]=> string(18) "employee2@test.com" } [3]=> array(2) { ["Aname"]=> string(9) "Employee3" ["Aemail"]=> string(18) "employee3@test.com" } }

The var_dump of $empemail: NULL
Post Reply