简体   繁体   中英

Basic contact form not displaying input info in email PHP HTML5

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 eg

../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?

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['....']);
    ...
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM