-1

Actually I was creating a simple form to send mail to my email ID. Here's my code:

<!doctype html>
<html>
    <body>
        <form>
            <input type='text' name='name'>
            <input type='email' name="email">
            <input type="number" name="mobile">
            <input type="text" name="message">
            <input type="submit" name='submit'>
        </form> 
    </body>
</html>

php code:

<?php
if($_POST['submit']!="")
{
 $cname = $_POST['name'];

 $email = $_POST['email'];
 $mob = $_POST['mobile'];
 $query = $_POST['message'];
 $subject="Enquiry";
 $message="Name : $cname \n  email : $email \n  Message : $query \n";
 $email_from = '<name@gmail.com.com>';
 $subject = "registration";
 $message = "You have received a new message from the user <b> $cname. </b> \n". "Here is the message:\n $message".
 $to = "name<name@gmail.com>";
 $headers = "From: $email_from \r\n";
 $headers.= "Reply-To: $email \r\n";
    if(mail($to, $subject, $message, $headers))
    {
     echo "<script>alert('Dear User, You Are Successfully Registered')</script>";
    }
}
?>

Note: In this question I have changed my actual email id with "name", in my actual code, the id is putten correctly. The error is in the first if statement that the 'submit' is undefined.

Ansh
  • 146
  • 1
  • 8

1 Answers1

0

If you load the page, $_POST is empty and $_POST["submit"] is undefined. If you click the submit button, $_POST["submit"] is set. So, instead of checking if the content of $_POST["submit"] is an empty string, check if the variable is set:

if( isset($_POST["submit]) ){
   // Form submitted, continue
}

Also, you forgot to set the method in your form:

<form method="post">
EinLinuus
  • 625
  • 5
  • 16