简体   繁体   中英

Why isn't my PHP form working? I do receive the email but there is no content in it

PHP form on my website isn't responding well. It would be appreciated if someone could help me.

This is the HTML Code of the form

<form action="contactform.php" method="post" name="form" class="p-5 bg-white">
    
    <div class="row form-group">
        <div class="col-md-6 mb-3 mb-md-0">
            <label class="text-black" for="fname">First Name</label>
            <input type="text" id="fname" class="form-control">
        </div>
        <div class="col-md-6">
            <label class="text-black" for="lname">Last Name</label>
            <input type="text" id="lname" class="form-control">
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <label class="text-black" for="email">Email</label> 
            <input type="email" id="email" class="form-control">
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <label class="text-black" for="subject">Subject</label> 
            <input type="subject" id="subject" class="form-control">
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <label class="text-black" for="mssg">Message</label>
            <textarea name="mssg" id="mssg" cols="30" rows="7" class="form-control" placeholder="Write your notes or questions here..."></textarea>
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <input type="submit" value="Send Message" class="btn btn-primary py-2 px-4 text-white">
        </div>
    </div>
</form>

This is the PHP Code

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $to = "alyyashar@gmail.com";
    $subject = "New email from your site!";
    $fname = $_POST['fname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
    $title = '<h3>Hello! You have received a new mail from your website!</h3>';
    
    $body = "$title
            <br/>
            <b>From:</b> $fname
            <br/>
            <b>E-Mail:</b> $email
            <br/>
            <b>Message:</b>\n$message
            <br/>
            <br/>";

    if (mail($to, $subject, $body, $headers)){
        echo "<h1>Sent Successfully! Thank you"." ".$fname.", We will contact you shortly!</h1>";
    } else { 
        echo "Something went wrong!";
    }
}
?>
<br>
<a href="/">Back to Homepage</a>

This is the email I receive

Screenshot of the email

When I enter information into the form and click send message, I do receive the email but there is no content in it.

The form elements have no name attributes, which is what the browser uses to send their name/value pairs to the server. So while it's posting the form, none of the values are being included.

For example, this:

<input type="text" id="fname" class="form-control">

Should be this:

<input type="text" id="fname" class="form-control" name="fname">

The same fix would need to be repeated for the remaining form elements.

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