2

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!"; 
        }    
?> 
Thanos
  • 3,039
  • 2
  • 14
  • 28
  • Have a look at [the difference between input type="button" and input type="submit"](http://stackoverflow.com/questions/290215/difference-between-input-type-button-and-input-type-submit). – Thibaud Colas Dec 05 '13 at 02:01
  • I don't know if it just a typo but you are closing the form tag and then the table tag, it should be the opposite. – Thanos Dec 05 '13 at 02:14
  • i have modified the code but it still does not work. – user3068297 Dec 05 '13 at 07:20
  • Add a `var_dump($_POST);` as the first line in your PHP script. This will show what your form is sending. Assuming your form is sending everything it should, you'll need to debug your PHP. Find the server logs (usually in `/etc/httpd/logs` for Linux machines) and see what they say. –  Dec 05 '13 at 23:27

2 Answers2

3

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.

0

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.

user3034378
  • 89
  • 1
  • 7