简体   繁体   中英

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. 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 
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 . You also need to give a name attribute to each field ( name is not the same as id ). These names should match the names you're using in your PHP script.

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

Make all the changes and see what happens. Don't expect your mail to be sent first time - PHP mail is notoriously fiddly. 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. 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.

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. This is important for security and to also avoid spam I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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