PHP form help.

They have: 10 posts

Joined: Nov 2011

First of all, I have zero knowledge and experience with PHP. I have been working on a website for about a year and need one form to mail to a different email address than all of the other site forms (GoDaddy has a PHP file that automatically sends the forms to my email). I am trying to create a form for time sensitive material (song request for a radio station) that will send to a different email address than GoDaddy has on file (my email).

I found this website: http://www.freecontactform.com/email_form.php with a form and php form mailer and attempted to edit it to match my form element names...

ERROR CODE:
Parse error: syntax error, unexpected T_STRING in D:\Hosting\7000100\html\gmsngrqst.php on line 51

CODE ON LINE 51:
$email_message = "Form details below.\n\n";

I cannot figure out how to fix the error and I do not know how to search for a solution online. I was very careful not to mess up any coding or defined values as I went through and changed element names. However, somewhere I created a syntax error that does not exist within the code I copied off the website (http://www.freecontactform.com/email_form.php).

MY PHP CODE (email address and subject removed):

<?php
if(isset($_POST['email'])) {
    
   
// EDIT THE 2 LINES BELOW AS REQUIRED
   
$email_to = "(email address removed";
   
$email_subject = "(subject line removed)";
    
    
    function
died($error) {
       
// your error code can go here
       
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo
"These errors appear below.<br /><br />";
        echo
$error."<br /><br />";
        echo
"Please go back and fix these errors.<br /><br />";
        die();
    }
    
   
// validation expected data exists
   
if(!isset($_POST['name']) ||
        !isset(
$_POST['location']) ||
        !isset(
$_POST['email']) ||
        !isset(
$_POST['artist']) ||
        !isset(
$_POST['title'])) {
       
died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
    
   
$name = $_POST['name']; // required
   
$location = $_POST['location']; // not required
   
$email_from = $_POST['email']; // required
   
$artist = $_POST['artist']; // required
   
$title = $_POST['title']; // required
    
   
$error_message = "";
   
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!
preg_match($email_exp,$email_from)) {
   
$error_message .= 'The Email you entered does not appear to be valid.<br />';
  }
   
$string_exp = "/^[A-Za-z .'-];
  if(!preg_match(
$string_exp,$name)) {
   
$error_message .= 'The name you entered does not appear to be valid.<br />';
  }
  if(!preg_match(
$string_exp,$artist)) {
   
$error_message .= 'The artist name you entered does not appear to be valid.<br />';
  }
  if(!preg_match(
$string_exp,$title)) {
   
$error_message .= 'The song name you entered does not appear to be valid.<br />';
  }
  if(strlen(
$error_message) > 0) {
    died(
$error_message);
  }
   
$email_message = "Form details below.\n\n";
    
    function clean_string(
$string) {
     
$bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace(
$bad,"",$string);
    }
    
   
$email_message .= "Name: ".clean_string($name)."\n";
   
$email_message .= "Location: ".clean_string($location)."\n";
   
$email_message .= "Email: ".clean_string($email_from)."\n";
   
$email_message .= "Artist: ".clean_string($artist)."\n";
   
$email_message .= "Song: ".clean_string($title)."\n";
    
    
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.
$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail(
$email_to, $email_subject, $email_message, $headers); 


<!-- include your own success html here -->

Thank you for your request!


}
?>

Any help I could get with this would be MOST appreciated!!!! Thank you for your time and consideration for helping me.

He has: 629 posts

Joined: May 2007

Hi, and welcome to the forums.

Instead of struggling with PHP and JavaScript, I suggest you look at the Wufoo form builder.

Cordially, David.

They have: 10 posts

Joined: Nov 2011

Thank you very much for this suggestion! I will look into it immediately.

TigerPrincess

They have: 10 posts

Joined: Nov 2011

It also doesn't pass this checker:

http://www.piliapp.com/php-syntax-check/

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I know this is an older post, and you may have found another solution, but for anyone interested the problem was on line #38 (based upon how it was pasted here):

$string_exp = "/^[A-Za-z .'-];

You are missing the closing quote, as well as the closing / to border the expression. [Edit: actually I followed the link to the tutorial you got this from, and there was even more missing from their example, here it is:

$string_exp = "/^[A-Za-z .'-]+$/";

If you look at how the code is formatted above, you will see on the line right after it, the if(!preg_match( is highlighted the same all text literals in the code before this point, giving a good clue as to where the error is.

This is where a good IDE that does syntax highlighting is very helpful. (myself, I use PhpED, but I program in PHP for a living, so it is worth the cost to me)

-Greg

They have: 10 posts

Joined: Nov 2011

I just wanted to add a little thank you message as I had not found any help on this issue (and had given up on any). Thank you SO MUCH for your help! I am very excited to try this change. Smiling

TigerPrincess

They have: 10 posts

Joined: Nov 2011

I tried my form, again and ran across more errors (lines 72 and 74) and removed:

<!-- include your own success html here -->

Thank you for your request!

I also added in a redirect, which seems to work Smiling My new problem is that I am not receiving any test submissions from the website (how frustrating). What is wrong with this code?

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = array("[email protected]","[email protected]");
    $email_subject = "website.com Song Request";

Any extra help on this matter would be MUCH appreciated!

Thank you!
TigerPrincess

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Hi, assuming that you did set the $email_to correct, then it looks like it should work correctly.

Have you checked to see if it ended up in spam/junk folder

Also, a note on this line:

@mail($email_to, $email_subject, $email_message, $headers);

There are very limited times where using the @ to suppress errors is valid programming technique. All other errors it is a crutch (ie. instead of properly programming to handle things, lets tell PHP to shut up when it complains)

This code just assumes everything was fine and dandy, try this instead so you know if the mail function even worked:

if (!mail($email_to, $email_subject, $email_message, $headers)) {
    die ("There was an error sending the mail");
}

If this error shows up, then you need to double check all your values you are passing to the mail function. if not then you need to check the junk/spam handling on your e-mails. Beyond that, would require some more in depth checking, that depending on setup, may not be able to do (ie. on my server, i would watch the mail logs to see if it actually sent out and see what result the receiving mail server gave.

-Greg

They have: 10 posts

Joined: Nov 2011

What does this mean?

Warning: mail() expects parameter 1 to be string, array given in D:\Hosting\7000100\html\gmsngrqst.php on line 69
There was an error sending the mail

Line 69 is the start of your suggested code. Is there an easier (yet safe) way I can get this form mailer to work?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

It means that $email_to is an actual array of values, and not a simple string, which is required for the mail function (this is probably what was killing your code to, but you had it set to hide the error).

Check to make sure that $email_to is set to just a single email address. ex:
$email_to = "[email protected]";

If you have more than one you need to send it to, you can use:
$email_to = "[email protected],[email protected]"; 

Note, no spaces, separated by commas

To test what you have, right before the mail() function, you can do:
var_dump($email_to);
and this will show you if it is a single string or an array of values.

-Greg

They have: 10 posts

Joined: Nov 2011

SUCCESS!! The multiple email address instructions fixed it completely. Thank you very much, and I hope this helps anyone else that has this difficulty =D

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Glad it is working. One other thing for in case you need it:

If you need to use BCC (so that it goes to an address, but the TO: recipients do not see that it is going there), you can add that in the following code that you have:

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

Also works for CC.

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'BCC: [email protected]'."\r\n" .
'CC: [email protected]'."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (!mail($email_to, $email_subject, $email_message, $headers)) {
    die ("There was an error sending mail");
}

(in the above example, for hosting clients who are not active with their sites and they want the service, I keep an eye on contact form submissions, so can make sure if anything urgent comes in, I can alert them to it for immediate attention.)

-Greg

They have: 9 posts

Joined: Sep 2014

I happen to be using the very same form that the lady above was using. However, I have recently switched my website platform over from php to shtml. I have renamed all of my pages to shtml. My previous php pages are loading whenever someone clicks on them from search engines. But, I am having a terrible time getting any php feedback form to work.

My domain is also with Godaddy and I have spoken with them. They said that they ran a test on my site with their own mail form and it functioned properly. They said that the problem lies within my script.

Well, the form itself loads perfectly, but once I fill out the form and click "submit" then I get the following error messages. What does all this mean?

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/shtml' . phpversion(); if (!mail($email_to, $email_subject, $email_message, $headers)) { die ("There was an error sending the mail"); } ?> Thank you for your feedback. 

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.