Connect with Expertise | Find Experts, Get Answers, Share Insights

How can I resolve my form mail script error?

 
Join Date: Jan 2010
Location: Africa
Posts: 1
#1: Jan 22 '10
This is the error message i got: (Parse error: syntax error, unexpected T_VARIABLE in /home/vocroyal/public_html/sendmail.php on line 12)

This is the full script detail:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   $fullname = $_REQUEST['fullname'] ;
  3.   $company = $_REQUEST['company'] ;
  4.   $address = $_REQUEST['address'] ;
  5.   $line1 = $_REQUEST['line1'] ;
  6.   $city = $_REQUEST['city'] ;
  7.   $country = $_REQUEST['country'] ;
  8.   $email = $_REQUEST['email'] ;
  9.   $phone = $_REQUEST['phone'] ;
  10.   $product = $_REQUEST['product'] ;
  11.   $order = $_REQUEST['order'] 
  12.   $comments = $_REQUEST['comments'] ;
  13.  
  14.   mail( "sales@vocroyalresources.com","Product Order",$fullname,  $company, $address,  $line1, $city, $country, $email, $phone, $product,  $order, $comments, "From: $email");
  15.  echo "<h2>Thank you for sending your order. We will get back to you!<p>Please Use the Back Button on the Left side of your Browser to Continue.</p></h2>";
  16. ?>

dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#2: Jan 22 '10

re: How can I resolve my form mail script error?


Correction again:
You forgot the semicolon at the end of: $order = $_REQUEST['order'] on line 11
(ergo the - unexpected T_VARIABLE ... on line 12)
see: http://onlamp.com/pub/a/php/2004/08/...uggingPHP.html


Correction: You should test for the existence of the $_REQUEST vars before assigning them to local vars, but the mail command; as stated, is wrong. I'll post a generic $_REQUEST assignment function soon...

There's nothing wrong with the code that transfers the $_REQUEST vars to local vars; other than being totally in-secure, but the mail(...) command is absolutely incorrect.

The mail command should be used like this:
Expand|Select|Wrap|Line Numbers
  1. $to      = 'nobody@example.com';
  2. $subject = 'the subject';
  3. $message = 'hello';
  4. $headers = 'From: webmaster@example.com' . "\r\n" .
  5.    'Reply-To: webmaster@example.com' . "\r\n" .
  6.    'X-Mailer: PHP/' . phpversion();
  7.  
  8. mail($to, $subject, $message, $headers);
  9.  
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#3: Jan 22 '10

re: How can I resolve my form mail script error?


Here's a generic $_REQUEST assignment function that's the beginning of a more robust user input validation routine.

Expand|Select|Wrap|Line Numbers
  1. // List of expected user input variables/values
  2. $expected = array('fullname','company','address');
  3.  
  4. /**
  5.  Step through the $_REQUEST array looking for expected input
  6.  and only assign to local variables if it's set.
  7.  If the expected variable is empty in the $_REQUEST array,
  8.  set the local variable to NULL
  9.  */
  10. foreach ($expected as $key) {
  11.   if ( !empty( $_REQUEST[ $key ] ) ) {
  12.     ${$key} = $_REQUEST[ $key ];
  13.   } else {
  14.     ${$key} = NULL;
  15.   }
  16. }
  17.  
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#4: Jan 23 '10

re: How can I resolve my form mail script error?


Note: I didn't mean to mess-up any newbies by using the $_REQUEST global in the example I provided above.

Normally you should use the appropriate $_GET or $_POST variables versus the $_REQUEST variable. The $_REQUEST variable also includes the $_COOKIE variable by default and could obviously cause clashes if you have variables with the same names in any of the other global variables.

As per this post: http://www.php.net/manual/en/reserve...uest.php#84527, you might want to use a switch block to determine the request method.

You'll notice that the code referenced in this posting is getting a reference to the targeted global array so the only cost is a memory location for the pointer.
Reply