简体   繁体   中英

PHP Contact Form Has No Content

The contact form I'm trying use is only sending the subject fields but no content.

I am using the following form:

<form id="contact-form-face" class="clearfix"      action="http://www.demo.com/php/contactengine.php">
                            <input type="text" name="email" value="Email"     onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
                            <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
                            <input class="contact_btn" name="submit"     type="submit" value="Send Message" />
                        </form>

And the PHP post:

<?php

$EmailFrom = "myemail";
$EmailTo = "myemail";
$Subject = "";
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) { 
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Any help would be greatly apprenticed

You're not defining a method on your form, this results in the "message" and "email" values being sent as GET parameters, this means they become query parameters as part of the URL. In order to get the form to send it's inputs to the $_POST, you must set the form action like so:

<form id="contact-form-face" class="clearfix" action="http://www.demo.com/php/contactengine.php" method="post">

Furthermore you have a typo on the recieving end, where you look for $_POST["Message"] but in the form you specify the name as "message". These must match.

//Edit - To implement an alert popup instead of a redirect change the if condition at the end of your script as follows:

if ($success) {
    ?>
    <script>
        alert("Success!");
    </script>
    <?php
}
else{
    ?>
    <script>
        alert("Failure!");
    </script>
    <?php
}

You are using

$_POST['Message']

It should be:

$_POST['message']

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