简体   繁体   中英

Contact Form Sending blank Data in PHP

I have recently made a website for my friend and contains a contact form using PHP. When the person sends the email, the data is not sent through the email.

This is the code for the website

<article class="grid_8">
    <div class="indent-left">
        <h3 class="p1">Contact us</h3>
        <form id="contact-form" method="post" name="myemailform" action="contact_form.php">  
            <fieldset>
                <label><span class="text-form">Name:</span><input name="name" type="text" required /></label>
                <label><span class="text-form">Email:</span><input name="email" type="text" required /></label>                              
                <label><span class="text-form">Phone:</span><input name="phone" type="text" /></label>                              
                <div class="wrapper">
                    <div class="text-form">Message:</div>
                    <div class="extra-wrap">
                        <textarea name="message" required></textarea>
                        <div class="clear"></div>
                        <div class="buttons">
                            <input type="submit" value="Submit" class="button">
                        </div> 
                    </div>
                </div>                            
            </fieldset>                     
        </form>
    </div>
</article>

This is the code for the PHP

<?php

$name = $_POST['name'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$email_from = $_POST['email'];
$email_subject = $_POST["New Message From www.chevroncreativesolutions.com"];
$email_body = $_POST["You have received a new message from $name.\n".
                     "Here is the message:\n $message.\n You Can call me on $phone.\n"].

$to = "matthias.mccarthy@gmail.com";
$headers = "From: $email_from \r\n";
$contact_form=mail($to,$email_subject,$email_body,$headers);

if($contact_form) {
    echo "Message has been sent.";
    echo "<form><input type='button' value='Return back' onclick='history.back();'></form>";
} else {
    echo "Error In sending your message";
    echo "<form><input type='button' value='Return back' onclick='history.back();'></form>";
}

You are setting your $email_subject and $email_body values to undefined $_POST values:

$email_subject = $_POST["New Message From www.chevroncreativesolutions.com"];
$email_body = $_POST["You have received a new message from $name.\n".
                     "Here is the message:\n $message.\n You Can call me on $phone.\n"];

Instead, drop the $_POST[] part:

$email_subject = "New Message From www.chevroncreativesolutions.com";
$email_body = "You have received a new message from $name.\n".
              "Here is the message:\n $message.\n You Can call me on $phone.\n";

Additionally, but unrelated to your issue, you'll want to sanitize the $email_from field to prevent header injection:

$email_from = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if ($email_from === false) {
    echo 'error, invalid email';
}

Your $headers = "From: $email_from \\r\\n"; must be "From: $EmailFrom" . "\\r\\n" "From: $EmailFrom" . "\\r\\n" .

You are missing a .

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