>> KYSCORP.COM TUTORIALS >> PHP Tutorials

PHP Contact Form

First, let's create the html section. I made a simple form, but it gets the job done. You can customize it if
you want, but remember to use the same form variables.


<form action="<?=$file_location;?>" method="post">
<input type="hidden" name="hidden" value="true">
<table>
<tr><td>From:</td><td><input type="text" name="from" value="" size="40"></td></tr>
<tr><td>Subject:</td><td><input type="text" name="subject" value="" size="40"></td></tr>
<tr><td>Message:</td><td><textarea name="message" cols="60" rows="10"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" style="font-weight: bold" value="Send"></td></tr>
</table>
</form>

Take a look

Now, for the PHP code, which should be placed above the HTML section.

This will be the first part of your code, which will, in term, determine if the form is being processed.
The isset function checks to see if the variable exist, this function has various alternatives.


<?php if(isset($hidden)){

After, if the form is indeed processed, check for all the variables using the isset function. You can alter the message
in the echo, if you feel like it. =)


if(!isset($from) or !isset($subject) or !isset($message)) {
echo("Your message couldn't be sent, because one or more fields weren't complete.");
}

We can finally use the mail function. Change the $to variable to your email address. Once again, you can edit the message in the echo!


else {
$to = "Your@email.com";
$from = "$from";
$subject = "$subject";
$message = "$message";
mail($to, $subject, $message,"From: $from");
echo("Your message was sent, I'll reply soon.");
}
}
?>

Final:


<?php
if(isset($hidden)){
if(!isset($from) or !isset($subject) or !isset($message)) {
echo("Your message couldn't be sent, because one or more fields weren't complete.");
} else {
$to = "Metalish@gmail.com";
$from = "$from";
$subject = "$subject";
$message = "$message";
mail($to, $subject, $message,"From: $from");
echo("Your message was sent, I'll reply soon.");
}
}
?>

<form action="<?=$file_location;?>" method="post">
<input type="hidden" name="hidden" value="true">
<table>
<tr><td>From:</td><td><input type="text" name="from" value="" size="40"></td></tr>
<tr><td>Subject:</td><td><input type="text" name="subject" value="" size="40"></td></tr>
<tr><td>Message:</td><td><textarea name="message" cols="60" rows="10"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" style="font-weight: bold" value="Send"></td></tr>
</table>
</form>


Enjoy !

By ~Metalish