Php联系表格不起作用?

时间:2016-10-05 21:21:25

标签: php html contact-form

我一直在尝试添加一个联系人到我的网站,但它似乎无法正常工作...提交表单时,它只是将我带到一个空白页面(在网址中输入详细信息),请参阅html和php表格如下。

HTML

<form action="contact.php">                 
     <input type="text" placeholder="Full Name" value="" name="name" />
     <br> <br>
     <select name="property">
         <option value="Commercial">Commercial</option>
         <option value="Residential">Residential</option>
     </select>
     <br> <br>
     <select name="work">
         <option value="Electrical">Electrical</option>
         <option value="Gas">Gas</option>
         <option value="Carpentry">Carpentry</option>
         <option value="Painting">Painting & decorating</option>
         <option value="Building">Building</option>
         <option value="Flooring">Floor fitting</option>
     </select>
     <br> <br>
     <input type="text" placeholder="Email Address" value="" name="email" />
     <br> <br>
     <input type="submit" value="Submit">
</form>

PHP

<?php

if(isset($_POST['email'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@here.co.uk";
    $email_subject = "CONTACT FORM APPLICATION";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
       !isset($_POST['email']) ||
       !isset($_POST['property']) ||
       !isset($_POST['work']))
        $name = $_POST['name']; // required
        $email = $_POST['email']; // required
        $property = $_POST['property']; // required
        $work = $_POST['work']; // required

    $email_message = "Form details below.\n\n";

    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "email: ".clean_string($email)."\n";
    $email_message .= "property: ".clean_string($property)."\n";
    $email_message .= "work: ".clean_string($work)."\n";

    // create email headers
    $headers = 'From: '.$email_from."\r\n".
               'Reply-To: '.$email_from."\r\n" .
               'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->
<center>
    <br><br><br>
Thank you for your enquiry, we will be in touch shortly<br><br> <img src="_include/img/smile.png"><br><br>You may now close you browser or go back to our home page by clicking <a href="index.html">here</a>
</center>

<?php  } ?>

我做错了什么?任何人都可以发布解决方案,以便我可以看到错误吗?

1 个答案:

答案 0 :(得分:0)

<form action="contact.php">

默认为GET,因此您需要:

<form action="contact.php" method="post">