0

My form having Name, Email, Phone & Message field is not working. When I am submitting the form, blank page is opening with the php mail handler url. It should be showing the thank you message instead. Also no email is received at the email ID. Please help.

My HTML form code is

<form id="contactform" action="contact_form.php" method="POST">
          <div>
            <input type="text" class="form-control" placeholder="Name" name="name" required id="name">
          </div>
          <div>
            <input type="email" class="form-control" placeholder="Email" name="email" required id="email">
          </div>
          <div>
            <input type="text" class="form-control" placeholder="Phone" required name="phone" id="phone">
          </div>
            <textarea class="form-control" name="message" id="message" rows="4" placeholder="Enter Your Message" required></textarea>

          <div class="text-center">
          <button type="submit" value="submit" class="btn btn-default">SUBMIT</button>
          </div>
        </form>

PHP CODE FOR MAIL

<?php 
if(isset($_POST['submit'])){
    $to = "xyz@abc.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = "Name:". $name . "\n" . "Phone:" . $phone . "\n" . "\n" . "Message:" . "\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . "\n" . "Name:" . $name . "\n" . "Phone:" . $phone . "\n" . "\n" . "Message:" . "\n" . $_POST['message'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you, " . $name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
    }
?>

The form is on the bottom of this page https://www.delveindia.com/products/billing-software/

1bunty1
  • 11
  • 5
  • if its on local then you need to do some more stuff. search how to send mail through localhost in php and you will get plenty of examples. – Alive to die - Anant Mar 16 '16 at 21:31
  • It's hosted on a Cpanel server. – 1bunty1 Mar 16 '16 at 21:32
  • Ask your server provider to do mail settings on your server. without that mail will not sent – Alive to die - Anant Mar 16 '16 at 21:33
  • I think mail setting is correct as I can receive mails submitted from this page https://www.delveindia.com/contact-us.php – 1bunty1 Mar 16 '16 at 21:42
  • You tagged this with [PHPMailer](https://github.com/PHPMailer/PHPMailer), but you're not using it, which would be a good idea as it would avoid the header injection attacks you're vulnerable to. You're also forging the from address; don't do that. – Synchro Mar 16 '16 at 21:51
  • peoples are not interested in helping others. I asked to mark because you said its' working, but no response. deleting my answer. – Alive to die - Anant Mar 18 '16 at 17:58

1 Answers1

2

I believe your button doesn't have a name. $_POST['submit'] cannot be set because there is nothing named submit.

Give your button the name, submit :)

DisplayName
  • 196
  • 3
  • 12