This form works and everything, but all I want is the comment box. I want to take away "name" and "email". I tried deleting those 2 lines from the top and the code from the form but then it doesn't work. I guess its because of this line: if ($_POST["email"]<>'')

Anyway, I'm very new at this and don't understand PHP too well but I do need to finish my website. Please help. Thanks.

<?php
if ($_POST["email"]<>'') {
    $ToEmail = 'youremail@site.com';
    $EmailSubject = 'Site contact form';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Name: ".$_POST["name"]."";
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."";
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
Member Avatar for diafol

You seem to be sending the data to itself (same file). You may find it easier to send the form to a dedicated formhandler file. Once the processing is done by the formhandler, it decides what to do next, e.g. redirect back to the form or go to another page, by using the header().

Yes, you're correct that line 2 is important. That's checking the email field to determine whether the form, or at least an email addres has been submitted.

You could modify your code to the following:

<?php
if (isset($_POST['comment']) && $_POST['comment']) {
    $ToEmail = 'youremail@site.com';
    $EmailSubject = 'Site contact form';
    $mailheader = "From: no-reply@site.com\r\n";
    $mailheader .= "Reply-To: no-reply@site.com\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Comment: ".nl2br($_POST["comment"])."";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.