PHP Form Mailer<

liunx

Guest
I was using the code Scoutt gave me a while back to check the form fields without using Java or anything. I was just redoing the web site today and finally decided to put it in. (redoing it for the last 5 hours, GAH)

Here is contact.php:

<?php $key = "gaming, game news, gaming news, gaming updates, game reviews, webmaster resource, webmasters, HTML guide, interesting news, weird news, news, web links, cool links, weird links, gaming";
include ("1.php"); ?>

<b>Contact Me</b><br>
The following form will send an email to the webmaster without the use of an email program (to avoid spam). Please be respectful. I welcome constructive criticism, comments, suggestions, etc.<br><br>

<form name="cform" method="post" action="send.php">
<font color="red">*</font> Your First Name: <br>
<INPUT TYPE="text" NAME="name" SIZE="40" class="form"> <br><br>
Your Email (for response): <br>
<INPUT TYPE="text" NAME="email" SIZE="40" class="form"> <br><br>
<font color="red">*</font> Message: <br>
<TEXTAREA NAME="msg" ROWS=6 COLS=40 class="form"></TEXTAREA><br><br>
<input type=submit class="form" value="Submit"">
<input type=reset class="form" value="Reset"><br>
</form>

<?php include ("2.php"); ?>

The $key thing is just for keywords so I can have different ones on each page. The include 1.php and 2.php are just for the header and the bottom.

Now send.php:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];

$recipient = "[email protected]";
$subject = "Nitrox.uni.cc Submission";
$headers = "From: $name\nReply-to: $email\nContent-Type: text/plain\n";

$date = date("Y-m-d, H:i:s");

if($_POST['name'] != ""){
$name = addslashes($_POST['name']);
} else {
echo "you forgot a field, hit your back button to redo it.";
}
if($_POST['msg'] != ""){
$name = addslashes($_POST['name']);
} else {
echo "you forgot a field, hit your back button to redo it.";
}

$message = "Sent by: $name\r\n
E-mail: $email\r\n
Message:\r\n
$msg\r\n
\r\n
Date of submission: $date\r\n
Remote IP: {$_SERVER['REMOTE_ADDR']}\r\n";

mail($recipient,$subject,$message,$headers);

header("Location: thanks.php");

?>

Last, thanks.php:

<?php $key = "gaming, game news, gaming news, gaming updates, game reviews, webmaster resource, webmasters, HTML guide, interesting news, weird news, news, web links, cool links, weird links, gaming"; include ("1.php"); ?>

Thankyou for your submission. It will be reviewed and responded to (if necessary).

<?php include ("2.php"); ?>

Now, the problem, heh. If you don't enter the name field or msg field, it says:

you forgot a field, hit your back button to redo it.you forgot a field, hit your back button to redo it.
Warning: Cannot modify header information - headers already sent by (output started at c:\webroot\apache\htdocs\technel\beta\send.php:16) in c:\webroot\apache\htdocs\technel\beta\send.php on line 34

Now it doesn't have the header of footer, so I also want that, but what's up with the Warning thing? Ok now, it works perfectly if you fill in the required fields.

Do you know how I can get rid of the error and when the error shows up, how I can have it include 1.php -error- 2.php like the rest of the pages so it's just not *blank*? Thanks!!!!!

MichaelSimple. The error comes up because you're trying to send
header information, after HTML output. You can avoid this
using exit() after the echo's, but in your case, it's
probably better to use a variable, like so:


<?php

$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];

$name = addslashes(trim($name));
$msg = addslashes(trim($msg));

$sendMail = true;

if(empty($name) || empty($msg)){
$sendMail = false;
}

if ($sendMail) {
$recipient = "[email protected]";
$subject = "Nitrox.uni.cc Submission";
$headers = "From: $name\nReply-to: $email\nContent-Type: text/plain\n";

$date = date("Y-m-d, H:i:s");

$message = "Sent by: $name
E-mail: $email
Message:
------
$msg
------
Date of submission: $date
Remote IP: {$_SERVER['REMOTE_ADDR']}\r\n";

mail($recipient,$subject,$message,$headers);
header("Location: thanks.php");
} else {
include('1.php');
echo "you forgot a field, hit your back button to redo it.";
include('2.php');
}

?>


Not tested, but should work fine.
BTW, I think I gave you most of this code ;)

Edit:
Fixed some other smaller errors in the code,
optimized it a bit, and added line-breaks in
my text, so you can read it more easily ;)
doesn't break
lines so that's why your post is so wide.It works flawlessly!!! Thanks!!!!!!!!!!!! You're a life saver!
 
Back
Top