Reputation: 142
I am trying to implement a simple yet effective contact form. The HTML for the page is below. Also the mail.php form I am using is listed below as well. What I want the contact form to do is require fields, and if the user did not fill in the required fields it sends them back to the same form with the error code. I think I'm close, but for some reason it's giving me an error code when submitting the form, filled out or empty. You can see it live, instead of using Apache server at darthvixcustomsabers.com and see. I also would love to be able to specify the cols/rows in css by using textarea, but that is not working either.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo <a href="contact.html"></a>;
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("[email protected]", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<!doctype html>
<html>
<head>
<title> DV Custom Sabers </title>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="style/kotorsale.css" />
<meta name="viewport" content="width=device-width" />
</head>
<div class="header"><a href="index.html">Darthvix Custom Sabers</a></div>
<div class="header1">DV Custom Sabers is the place to go for your saber needs!</div>
<div class="logo"><a href="index.html"><img src="images/logo.png" alt="schedule" height="200" width="350" border="0"></a></div>
<ul id="nav">
<li><a href="index.html">Home</a></li>
<li><a href="aboutme.html">About Me</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="Gallery.html">Gallery</a></li>
<li><a href="kotorsale.html">For Sale</a></li>
<li><a href="buildlog.html">Build Log</a></li>
<li><a href="contact.html"> Contact Us</a></li>
</ul>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
</div>
</body>
</html>
Upvotes: 1
Views: 711
Reputation: 81
Hi why you are using this line of code?
<form action="mail.php" method="POST" enctype="multipart/form-data">
you are sending only the data there is no image of file to be send, its better use this
<form action="mail.php" method="POST">
remove the enctype . enctype is use only if there is an image or file ... use the correct tag
Upvotes: 1
Reputation: 116
You haven't quite presented your information in a clear manner, but hopefully I understand your problems correctly.
First, your form action in contact.html needs to point to your PHP email script. The following:
<form action="" method="POST" enctype="multipart/form-data">
Should be:
<form action="mail.php" method="POST" enctype="multipart/form-data">
You are missing quotes in the following echo statement:
echo <a href="contact.html"></a>;
This should be:
echo '<a href="contact.html"></a>';
Also, if you are trying to redirect the user back to the form if any fields are blank, then you will need more than just an empty link displayed on the page. An easy way would be to replace the following:
echo '<a href="contact.html"></a>';
With:
header( 'location:contact.html' );
This will direct the user back to the origin contact form. Unfortunately, this would be confusing to a user. You may want to change contact.html to contact.php so you can leverage PHP to display error messages.
Upvotes: 0