PHP Contact Mail Form

I know I have another of these posted on my blog however that one assumes that php global settings is OFF and many servers have it set to ON. So here is one that should work regardless of your server settings. To see an example of this working, just click on the contact page of my website in the links above.

Code:

<html>

<body>

<?php

//if submit button pressed

if(isset($_POST[‘submit’])){

//sending to my email
$name = $_POST[‘name’];
$email = $_POST[’email’];
$comments = $_POST[‘comments’];
$subject = $_POST[‘subject’];
$sql = “‘$name’, ‘$email’, ‘$comments'”;
$em = “‘$subject'”;
mail(‘enterYourEmailAddressHere’, $em, $sql);

echo(“<p>You message has been sent!” . “</p>”);
}
?>

<form name=”form1″ id=”form1″ method=”post” action=”<?=$PHP_SELF?>”>
<p align=”left”><strong>Name:</strong>
<input type=”text” name=”name” />
</p>
<p align=”left”>&nbsp;</p>
<p align=”left”><strong>Email:</strong>
<input type=”text” name=”email” />
</p>
<p align=”left”>&nbsp;</p>
<p align=”left”><strong>Subject:</strong>
<input type=”text” name=”subject” />
</p>
<p align=”left”>&nbsp;</p>
<p align=”left”><strong>Message:</strong>
<textarea name=”comments” cols=”60″ rows=”3″></textarea>
</p>
<p align=”left”>
<input name=”submit” type=”submit” id=”submit” value=”submit” />
<input type=”reset” name=”Submit2″ value=”Reset” />
</p>
</form>

</body>

</html>

php: Contact Page

for those of you interested in creating a contact page with a form (fields and submit button) that goes directly to your email, check this out. You simply need to copy and paste this code into the body of your html. You also need to put your email address in the part that says ‘YourEmailAddressHere’ and need to save the page as something like: contact.php, not something like contact.html. And thats it. Very easy to do.

<form name=”form1″ id=”form1″ method=”post” action=”<?=$PHP_SELF?>”>
<p>Name:
<input type=”text” name=”name” />
</p>
<p>&nbsp;</p>
<p>Email:
<input type=”text” name=”email” />
</p>
<p>&nbsp;</p>
<p>Subject:
<input type=”text” name=”subject” />
</p>
<p>&nbsp;</p>
<p>Message:
<textarea name=”comments” cols=”40″></textarea>
</p>
<p>&nbsp;</p>
<p>
<input name=”submitentry” type=”submit” value=”SUBMIT” />
<input type=”reset” name=”Submit2″ value=”Reset” />
</p>
</form>
<p>
<?php
if ($submitentry == “SUBMIT”){
$sql = “‘$name’, ‘$email’, ‘$comments'”;
$em = “‘$subject'”;
mail(‘YourEmailAddressHere’, $em, $sql);
echo(“<p>Your message has been sent!” . “</p>”);
}
?></p>