联系表单php邮件代码不起作用


contact form php mail code is not working

这是我的网站:http://www.iamshahil.com,我的索引页面中有一个联系人表单,我希望将联系人表单中提交的数据发送到我的电子邮件中。我有一个代码,但它不起作用,我的提交按钮无法点击。

这是我的表格:

<form method="post" action="mail.php">
    <table>
        <tr>
            <td class="feedbacktext">Name</td>
        </tr>
        <tr>
            <td> <input type="text" id="name" placeholder="You"></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr style="margin-top:20px;">
            <td class="feedbacktext">Email</td>
        </tr>
        <tr>
            <td><input type="text" id="email" placeholder="you@email.com"></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td class="feedbacktext">Website</td>
        </tr>
        <tr>
            <td><input type="text" id="website" placeholder="http://www.yourwebsite.com"></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td class="feedbacktext">Whats up?</td>
        </tr>
        <tr>
            <td><textarea  id="message" placeholder="whats up?" rows="4" cols="30"></textarea></td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td><input id ="send" class="button" type="button" value="Send" style="float: right;"></td>
        </tr>
</form>
</table>

这是我的php代码:

    <?php 
if(isset($_POST['submit']))
    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
  $website=$_REQUEST['website']; 
    $message=$_REQUEST['message']; 
    if (($name=="")||($email=="")||($message=="")) 
        { 
        echo "All fields are required, please fill <a href='"'">the form</a> again."; 
        } 
    else{         
        $from="From: $name<$email>'r'nReturn-path: $email"; 
        $subject="Message sent using your contact form"; 
        mail("me@gmail.com", $subject, $message, $from); 
        echo "Email sent!"; 
        }    
?> 

开始:将按钮的type更改为submit。您还需要为每个字段指定一个name属性(name,而不是id相同)。这些名称应该与您在PHP脚本中使用的名称相匹配。

例如:

<input type="text" id="name" placeholder="You">

应该成为

<input type="text" id="name" placeholder="You" name="name">

您应该在PHP脚本中将其称为$_REQUEST['name']

做所有的改变,看看会发生什么。不要指望你的邮件是第一次发送的——PHP邮件是出了名的麻烦。预计必须对其进行一点调试。

正如Mike W所说,您需要设置要提交的按钮的类型,并为输入字段添加name属性。name属性是您在php代码中的if语句中使用的属性,用于检查用户在提交表单之前是否填写了输入字段。

我还想指出的是,您应该将$_REQUEST替换为$_POST,因为您知道您在表单中使用的是"POST"方法。我认为这对安全性和避免垃圾邮件都很重要。