PHP Help with Error

Hero

─║╣ero─
Reaction score
250
Hey everyone I'm still learning PHP but I designed a script and I am getting an error.

Any help?

The error says:

Code:
Parse error:  parse error, unexpected $ in hsscontact.php on line 61

Here's the code:

PHP:
<?php
$sendto = '[email protected]';
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Address'];
$city = $_REQUEST['City'];
$state = $_REQUEST['State'];
$zip = $_REQUEST['ZipCode'];
$homephone = $_REQUEST['HomePhone'];
$homeowner = $_REQUEST['HomeOwner'];
$headers = "From: $from";

$subject = "Alarm Application Contact Form";

$fields = array();
$fields{"FirstName"} = "First Name";
$fields{"LastName"} = "Last Name";
$fields{"Address"} = "Address";
$fields{"City"} = "City";
$fields{"State"} = "State";
$fields{"ZipCode"} = "ZipCode";
$fields{"HomePhone"} = "Home Phone";
$fields{"SecondaryPhone"} = "Secondary Phone";
$fields{"SpouseName"} = "Spouse's Name";
$fields{"HomeOwner"} = "Home Owner";
$fields{"Email"} = "Email";
$fields{"Agent_Referral"} = "Agent Referral";


$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: [email protected]";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.financialfreedomus.com/alarm";

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 {
if($address == '') {print "You have not entered a address, please go back and try again";}
else {
if($city == '') {print "You have not entered a city, please go back and try again";}
else {
if($state == '') {print "You have not entered a state, please go back and try again";}
else {
if($zip == '') {print "You have not entered a zip code, please go back and try again";}
else {
if($homephone == '') {print "You have not entered a home phone, please go back and try again";}
else {
if($homeowner == '') {print "You have not selected whether or not if you are a home owner, please go back and try again";}
else {

$send = mail($sendto, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.financialfreedomus.com/alarm" );}
else
{print "We encountered an error sending your mail, please notify [email protected]"; }
}
}
?>

Any help is appreciated.
 

The Helper

Necromancy Power over 9000
Staff member
Reaction score
1,697
I dont do PHP but I would look at this line

$headers = "From: $from";

That $from looks like a variable and none of the languages I know want a variable in string quotes. It is probably seeing that $from and blowing up but that is just my best guess.
 

Hero

─║╣ero─
Reaction score
250
I dont do PHP but I would look at this line

$headers = "From: $from";

That $from looks like a variable and none of the languages I know want a variable in string quotes. It is probably seeing that $from and blowing up but that is just my best guess.

I tried that but I didn't seem to work.

+Rep for the help anyway.
 

The Helper

Necromancy Power over 9000
Staff member
Reaction score
1,697
Try this

change

$headers = "From: $from";

to

$headers = "From: " $from;

I don't what PHP uses for String Concatenation but if you know what that is you should put it after "From: "

You also have an extra space on the following two lines before the semi colons (I have no idea whether that matters or not)

$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
 

Hero

─║╣ero─
Reaction score
250
Try this

change

$headers = "From: $from";

to

$headers = "From: " $from;

I don't what PHP uses for String Concatenation but if you know what that is you should put it after "From: "

You also have an extra space on the following two lines before the semi colons (I have no idea whether that matters or not)

$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;

Tried it but it didnt work :(

Hopefully someone helps figure out the problem..lol
 
Reaction score
333
Like Perl, PHP allows you to express variables in double quoted strings.

The error is caused by malformed else clauses that are not being closed properly. Here is a fixed version of the original code (I used the interpreter on codepad.org):

PHP:
<?php
$sendto = '[email protected]';
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Address'];
$city = $_REQUEST['City'];
$state = $_REQUEST['State'];
$zip = $_REQUEST['ZipCode'];
$homephone = $_REQUEST['HomePhone'];
$homeowner = $_REQUEST['HomeOwner'];
$headers = "From: $from";

$subject = "Alarm Application Contact Form";

$fields = array();
$fields{"FirstName"} = "First Name";
$fields{"LastName"} = "Last Name";
$fields{"Address"} = "Address";
$fields{"City"} = "City";
$fields{"State"} = "State";
$fields{"ZipCode"} = "ZipCode";
$fields{"HomePhone"} = "Home Phone";
$fields{"SecondaryPhone"} = "Secondary Phone";
$fields{"SpouseName"} = "Spouse's Name";
$fields{"HomeOwner"} = "Home Owner";
$fields{"Email"} = "Email";
$fields{"Agent_Referral"} = "Agent Referral";


$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: [email protected]";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.financialfreedomus.com/alarm";

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 {
if($address == '') {print "You have not entered a address, please go back and try again";}
else {
if($city == '') {print "You have not entered a city, please go back and try again";}
else {
if($state == '') {print "You have not entered a state, please go back and try again";}
else {
if($zip == '') {print "You have not entered a zip code, please go back and try again";}
else {
if($homephone == '') {print "You have not entered a home phone, please go back and try again";}
else {
if($homeowner == '') {print "You have not selected whether or not if you are a home owner, please go back and try again";}
else {

$send = mail($sendto, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.financialfreedomus.com/alarm" );}
else
{print "We encountered an error sending your mail, please notify [email protected]"; }
}
}
}
}
}
}
}
} # many extra '}'s
?>

However your current style and indentation make it quite hard to read the code and spot errors like this. If I might suggest something slightly better and the use of elseif..

PHP:
<?php
$sendto = '[email protected]';
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Address'];
$city = $_REQUEST['City'];
$state = $_REQUEST['State'];
$zip = $_REQUEST['ZipCode'];
$homephone = $_REQUEST['HomePhone'];
$homeowner = $_REQUEST['HomeOwner'];
$headers = "From: $from";

$subject = "Alarm Application Contact Form";

$fields = array();
$fields{"FirstName"} = "First Name";
$fields{"LastName"} = "Last Name";
$fields{"Address"} = "Address";
$fields{"City"} = "City";
$fields{"State"} = "State";
$fields{"ZipCode"} = "ZipCode";
$fields{"HomePhone"} = "Home Phone";
$fields{"SecondaryPhone"} = "Secondary Phone";
$fields{"SpouseName"} = "Spouse's Name";
$fields{"HomeOwner"} = "Home Owner";
$fields{"Email"} = "Email";
$fields{"Agent_Referral"} = "Agent Referral";


$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: [email protected]";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.financialfreedomus.com/alarm";

if($from == '') {

  print "You have not entered an email, please go back and try again";
} elseif ($name == '') {
  print "You have not entered a name, please go back and try again";
} elseif($address == '') {
  print "You have not entered a address, please go back and try again";
} elseif($city == '') {
  print "You have not entered a city, please go back and try again";
} elseif($state == '') {
  print "You have not entered a state, please go back and try again";
} elseif($zip == '') {
  print "You have not entered a zip code, please go back and try again";
} elseif($homephone == '') {
  print "You have not entered a home phone, please go back and try again";
} elseif($homeowner == '') {
  print "You have not selected whether or not if you are a home owner, please go back and try again";
} else {
  $send = mail($sendto, $subject, $body, $headers);
  $send2 = mail($from, $subject2, $autoreply, $headers2);
  if($send) {
    header( "Location: http://www.financialfreedomus.com/alarm" );
  } else {
      print "We encountered an error sending your mail, please notify [email protected]";
  }
}
?>
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
You are making sure that user-provided input written into the email header doesn't contain any carriage returns or loads of email addresses, aren't you?

It'd be really bad if someone could misuse your form to send additional spam in a single submission, compared to what's already possible with this code.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top