1

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("horgantm@gmail.com", $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>
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Terry
  • 142
  • 4
  • 16
  • Simple yet effective! That's always good. – bjb568 Oct 24 '13 at 03:10
  • So it's performing the response to an empty field, even when all fields are filled? – Eamonn Oct 24 '13 at 03:13
  • What is this error code you speak of? Could be useful... – Machavity Oct 24 '13 at 03:14
  • @Machavity I've never seen a 405 response before: `405 - HTTP verb used to access this page is not allowed.` –  Oct 24 '13 at 03:14
  • Mike W is probably right. After a quick google I found [this](http://www.checkupdown.com/status/E405.html) "You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form." – bjb568 Oct 24 '13 at 03:16
  • Who's server are you using? – bjb568 Oct 24 '13 at 03:18
  • Mike W is most likely correct if it's a 405 error. Also, you don't need the extra 'action' field, if all you're using it for is to check if the form has been submitted. Just use `isset` or `empty` on one of the other fields, or on the $_POST array itself. – Eamonn Oct 24 '13 at 03:19
  • 1
    It could be because the index page is being seen as a static HTML page. Is your file named index.php? http://stackoverflow.com/questions/6841139/server-error405-http-verb-used-to-access-this-page-is-not-allowed – Machavity Oct 24 '13 at 03:31
  • @MikeW and folks Anybody gonna post an answer? Comments aren't "official". – bjb568 Oct 24 '13 at 03:40
  • I noticed a couple of inconsistencies in the HTML when I was formatting your code. There's no opening tag, and you have an extra closing near the end (all the
    tags are closed before that).
    – Adi Inbar Oct 24 '13 at 03:40
  • go daddy is my host. if I copy the mail.php content into the index file and make it index.php will that help? I also want it to require the fields though or it won't let it send. – Terry Oct 24 '13 at 04:04
  • Looked at problem again. Form is submitting a POST to an HTML page so (probably) server is rejecting it. Form needs to send its data to PHP script. It's not clear in the code posted whether that's one file or two. Don't have more time to spend on this. –  Oct 24 '13 at 05:06
  • each set of code is one file so the HTML is one file and then the php is another. I'll try combining and naming index.HTML to .php and see what happens . – Terry Oct 24 '13 at 12:31

2 Answers2

1

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

hello
  • 81
  • 1
  • 12
0

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.