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

Ali Ashar

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 = "[email protected]";
    $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.

David

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why isn't my PHP form working?

Why isn't href working on my content?

Why isn't my form working?

How do I check users email isn't already in my database when submitting a form?

Why isn't my regex email validation working?

Why isn't my form saving but working with the API only?

Why isn't my Create Form working, or even responding?

PHP error: Why isn’t this working, I can't see data result in my page?

I don't know why my 'if' statement isn't working

Why isn't my function save_users(): working? How do I save these passwords to file?

Why isn't my PHP working when I try to count the number of rows in a table?

Why is my global variable isn't working PHP

Why isn't my Where working as I think it should?

Why isn't my movement working after I put it in a function?

I'm trying to do a search with php and sql for an ecommerce website, why isn't this code working?

PHP form isn't sending email to recipients

Why isn't my form working correctly when trying to post something using Ajax and an external PHP file, which is receiving two strings?

Why isn't utorrent working? How do I remedy this problem?

How do I get the count of loans? Why isn't this working?

why isn't this login form code working?

Why isn't my background-color not working on my HTML form in CSS?

Why isn't my <h:form> visible?

Why isn't my React Form sending?

Why isn't my form submitting

Why isn't my monkey patching isn't working ?

Why isn't my DHCP server working?

Why isn't my inline working?

Why isn't onClick working for my button?

Why isn't my BXSlider working?