0

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 = "sarah@blockcpm.com";

    $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?

MIRMIX
  • 1,052
  • 2
  • 14
  • 40
spbrad
  • 33
  • 7

2 Answers2

1

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['....']);
    ...
}
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
  • That was the problem! Many thanks! And thanks for realising that this was not actually a duplicate. – spbrad Jun 27 '17 at 10:36
1

You have not set the method of the form so it defaults to making a GET request.

The data you can see in the URL will be used to populate $_GET.

You need to set method="POST" on the <form> element to make a POST request, cause the data to be put in the request body, and appear in $_POST.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335