Jump to content

Simple PHP Contact Form


BenJancewicz

Recommended Posts

Hi everyone,

I have a pretty simple PHP contact form I've been working on, but for the life of me I can't figure out why it's not working.

 

The thing doesn't spit any errors out to me, but I don't get the email when I run tests.

 

Here's the code:

 

  <?
// Takes posts from form and turns it into an array
$fields = $_POST;

// Makes the submit button usable again, incase the form gets stuck
unset($fields['submit']);

// Sets the filename of the DSV file
$file = "contact.csv";

// Opens the file (or error if it can't open it)
$fh = fopen($file,'a') or die("can't open file");

// Puts a quote around the commas so that they turn into columns
$delim = '","';

// Writes the commas from the line above
$csv_row = implode($delim, $fields);

// Puts a quote in front of the beginning of the first field
fwrite($fh, '"');

// Writes the data from the line above
fwrite($fh, $csv_row);

// Puts a quote at the end of the last field
fwrite($fh, '"');

// Sets up "Carriage Return Line Feed" (CRLF) (basically ENTER) or in HTML, <br>
$crlf="\r\n";

// Writes the line above
fwrite($fh, $crlf);

// Closes the file
fclose($fh);



PRINT "<p>";

echo "<center>
Thank you!
      </center>";

$message .= "\nName: $name\r\n
         \nEmail: $email\r\n
         \nComment: $comment\r\n
";

$headers .= "From: $email <email>\r\n";

mail("[email protected]", "Zerflin Contact Form", $message, $headers);

?>

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/110789-simple-php-contact-form/
Share on other sites

put error_reporting(E_ALL) at the top, and above that, put ini_set('display_errors', 'On')

 

Then, see if it says any errors.

Also:

 

// Takes posts from form and turns it into an array

$fields = $_POST;

 

// Makes the submit button usable again, incase the form gets stuck

unset($fields['submit']);

 

Post is already an array.... You're simply copying it to another variable. You could access it straight like $_POST['submit'].

 

Also, you don't need to unset the submit button since POST data isn't persistent. Each new request, the POST array is reset. Also, you're not unsetting the POST array key of submit, you're unsetting the copy.... When ever you make a variable each to another variable, it makes a copy of it. (Actually, it doesn't make a copy until you modify it... Until then it's a reference, but...)

 

 

Also, while I'm getting technical, PRINT doesn't need to be uppercase, and it should probably be all lower case.

 

"$message .= "\nName: $name\r\n

        \nEmail: $email\r\n

        \nComment: $comment\r\n

";

 

$headers .= "From: $email <email>\r\n";"

 

Neither of those string have been previously set, so you shouldn't be using the concatenation equals operator...  You should simply be using equal...  PHP throws a notice over that.  (Or maybe warning....  I bet it's a notice.)

 

Alright. I've done what you suggested (I think.. I'm sorry, I'm very new at this).

 

Now I get an error

Parse error: syntax error, unexpected T_STRING in /home/content/b/j/a/bjancewicz/html/contact/contact.php on line 19

 

Here's what the code looks like now:

 

  <?
  

ini_set('display_errors', 'On')
error_reporting(E_ALL)
  
// Takes posts from form and turns it into an array
$fields = $_POST['submit'];

// Makes the submit button usable again, incase the form gets stuck
unset($fields['submit']);

// Sets the filename of the DSV file
$file = "contact.csv";

// Opens the file (or error if it can't open it)
$fh = fopen($file,'a') or die("can't open file");

// Puts a quote around the commas so that they turn into columns
$delim = '","';

// Writes the commas from the line above
$csv_row = implode($delim, $fields);

// Puts a quote in front of the beginning of the first field
fwrite($fh, '"');

// Writes the data from the line above
fwrite($fh, $csv_row);

// Puts a quote at the end of the last field
fwrite($fh, '"');

// Sets up "Carriage Return Line Feed" (CRLF) (basically ENTER) or in HTML, <br>
$crlf="\r\n";

// Writes the line above
fwrite($fh, $crlf);

// Closes the file
fclose($fh); 



print "<p>";

echo "<center>
Thank you!
      </center>";

$message .= "\nName: $name\r\n
		\nEmail: $email\r\n
		\nComment: $comment\r\n
";

$headers .= "From: $email <$email>\r\n";

mail("[email protected]", "Zerflin Contact Form", $message, $headers);

?>

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.