简体   繁体   English

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

[英]contact form php mail code is not working

This is my website : http://www.iamshahil.com , I have a contact form in my index page and i want the submitted data from the contact form to be sent to my email. 这是我的网站: http : //www.iamshahil.com ,我在索引页面上有一个联系表,我希望将联系表中提交的数据发送到我的电子邮件中。 i have a code but that does not work and my submit button is not being able to be clicked. 我有一个代码,但是它不起作用,并且无法单击我的提交按钮。

Here is my form : 这是我的表格:

<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>

and here is my php code : 这是我的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!"; 
        }    
?> 

To start: change the type of your button to submit . 要开始:将按钮的type更改为submit You also need to give a name attribute to each field ( name is not the same as id ). 你还需要给一个name属性,每个字段( name一样的id )。 These names should match the names you're using in your PHP script. 这些名称应与您在PHP脚本中使用的名称匹配。

For example: 例如:

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

should become 应该成为

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

and you should refer to it in your PHP script as $_REQUEST['name'] 并且您应该在PHP脚本中将其称为$_REQUEST['name']

Make all the changes and see what happens. 进行所有更改,看看会发生什么。 Don't expect your mail to be sent first time - PHP mail is notoriously fiddly. 不要期望您的邮件会在第一时间发送-众所周知,PHP邮件很奇怪。 Expect to have to debug it a bit. 期望不得不调试一下。

Just like what Mike W said, you need to set the type of the button to submit and also add the name attribute for the input fields. 就像Mike W所说的一样,您需要设置要提交的按钮的类型,还需要为输入字段添加name属性。 The name attribute is what you use in your if statement in you php code to check whether the user filled an input field or not before submitting the form. name属性是您在php代码中的if语句中使用的属性,用于在提交表单之前检查用户是否填写了输入字段。

I also want to point out that you should replace $_REQUEST with $_POST , since you know u are using the 'post' method in your form. 我还想指出,您应该将$ _REQUEST替换 $ _POST ,因为您知道您在表单中使用的是'post'方法。 This is important for security and to also avoid spam I believe. 我认为,这对于确保安全以及避免垃圾邮件非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM