Basic contact form does not display the input information in the PHP HTML5 email

advertisements

I have a basic contact form submitted using PHP. When it is submitted I receive an email however it does not display the information input into the field. That information is however sent. It displayed in the address bar of the browser e.g.

../contact.phpurl=antispam&name=dfsaa&number=fdfd&email=fdsafds%40fds.com&message=Please+contact+me+regarding+this

Front:

<form action="contact.php">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
<input class="form-control" id="number" name="number" placeholder="Contact Number" type="text" required>
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
<textarea class="form-control" id="message" name="message" placeholder="Message" rows="5">Please contact me regarding this...</textarea><br>
<button class="btn btn-default pull-right" type="submit">Send</button>
</form>

PHP:

    <?php
    $name = @trim(stripslashes($_POST["name"]));
    $email = @trim(stripslashes($_POST["email"]));
    $number = @trim(stripslashes($_POST["number"]));
    $message = @trim(stripslashes($_POST["message"])); 

    $email_from = $email;
    $email_to = "[email protected]";

    $body = "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" . "Number: " . $number . "\n\n" . "\n\n" . "Message: " . $message;

    $success = @mail($email_to, $body, "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" . "Number: " . $number . "\n\n" . "Message: " . $message);

 ?>

 <!DOCTYPE HTML>
 <html lang="en-US">

 <head>
    <script>
        alert("Thank you for contacting us. A member of our team will be in touch as soon as possible.");

    </script>
    <meta HTTP-EQUIV="REFRESH" content="0; url= index.html">
  </head>

The contact form works perfectly on another site... This one is on a godaddy site that i didnt create, i'm just ammending.

The email that i receive has all of the field titles but none of the input info.

Any ideas why?


Your form is by default of type GET, not POST as you want to check in PHP therefore data are visible through $_GET and not $_POST global PHP variable.

Since you want POST, modify your form structure

<form action="contact.php">

should be

<form action="contact.php" type="post">

And in your PHP, first check if request method is POST

<?php
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
    //Get data and send email
    $name = trim($_POST['....']);
    ...
}