Forum Moderators: coopster

Message Too Old, No Replies

Sending Mail with XHTML and PHP

I know it's been covered but I can't get it!

         

gometro33

11:43 pm on Jul 4, 2005 (gmt 0)



I'm new to both the forum and to making websites. I'm currently trying to make a "comment" for where people can send comments, questions, gripes, etc. The website, at least for now, is intended to be just for friends, but also as a test for my (X)HTML/PHP skill (the little that I have...). Basically, I want to have the people fill out a for where they enter their name and email, a subject and their message. This is the form that I have:

<div>
<form id="form" action="contact3.php" method="post">
Name: <br /><input id="name" type="text" size="30" /><br />
E-mail: <br /><input id="email" type="text" size="30" /><br />
Subject: <br /><input id="subject" type="text" size="30" /><br />
Your comments: <br /><textarea id="comment" rows="10" cols="30" />
<br />
<br />
<input id="submit" type="submit" value="Send" />
<input id="reset" type="reset" value="Reset" />
</form>
</div>

I should note here: I've been doing this for about a week so if there is something missing that only a noob could miss, please tell me because I probably didn't notice it.

So the form is pretty basic. I'm using XHTML Strict and when I try to validate the page this is the only part that comes up as invalid (but just about everything in it is noted as an error). I can't see anything wrong with the code. Does anyone else see anything?

This is the PHP code I have:


<?php
//Define variables
$to = "myemail@address.com";
$subject = $_POST['subject'];
$ip = $_SERVER['REMOTE_ADDRESS'];
$name = $_POST['name'];
$from = $_POST['email'];
$comments = $_POST['comments'];
//Create message
$message = "Name: $name\n";
$message .= "E-mail: $from\n";
$message .= "IP: $ip\n\n";
$message .= "Comments: $comments";
//Create header
$header = "From: $name<$from>\n"
if ($_POST['submit']) {
if ($name == "" ¦¦ $email == "" ¦¦ $subject == "" ¦¦ $comments == "")
{
echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>";
}
}
else {
mail($to, $subject, $message, $header)
}
?>

Again, pretty basic stuff. Any insight into how I can get this working would be great. I would rather write my own form and script rather than copy it from someone so that I can try to learn this stuff.

I had a question about PHP in general also. I may have missed this in the tutorials that I read about PHP but how exactly do I set up the file. I know that it goes in the <body> section but do I need to put the <html> and <head> tags and a DTD and all that good stuff? Sorry for such basic questions. Thanks in advance.

coopster

2:19 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, gometro33.

PHP can be invoked and parsed anywhere within your file (or script). Yes, you still need to generate valid HTML though, so your document should have all the necessary requirements according to the standard you are coding, including the DTD, <html>, <head>, etc. information.