PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP PHP Contact Form
Your Ad Here

PHP Contact Form


PHP Contact FormFirst, 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>

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 !



Author's URL: Kyscorp.com
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "PHP Contact Form"

Only registered users can write comment

No comments yet...