0

我正在尝试将 php 添加到我的联系我们页面,以确保消息至少有几个字符长才能发送。我试过 if (strlen()> 2) 但它只是让消息通过。enter code here

<div id="phillya11">
    <a href="#" style="text-decoration:none;color:#FFF5ee">
        Contact us
    </a>
</div> 

<div id="soon4">
    <form action="index.php" method="POST">
        <p>Name</p> <input type="text" name="name">
        <p>Email</p> <input type="text" name="email">
        <p>Message</p>
        <textarea style="width:475px; height:175px;margin-left:7%" name="message"></textarea><br />
        <input type="submit" value="Send"><input type="reset" value="Clear">
    </form>
</div> 

PHP:

<?php 
    if (strlen($message)> 2)
        $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "myemail";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!";
?>

jQuery:

$("#phillya11").click(function () 
{
    $("#soon4").show();
    $("#soon").hide();
    $("#soon1").hide();
    $("#soon2").hide();
    $("#soon3").hide();    
    $('#close4').click(function () 
    {
        $("#soon4").hide();
    });
4

4 回答 4

1

$message尚未设置,因此 if 语句将失败。将其更改为发布消息。在检查字符串长度之前,您还需要检查消息是否已设置。

还要注意使用{ }withif语句,当语句为真时,花括号之间的内容将执行。

<?php
  //$message  is not set here until inside the if statment
  //there for we should check $_POST['message']
  //if $_POST['message'] has the criteria you seek
  //THEN $message will be set as shown at line 5 which i marked for you to see

  if (isset($_POST['message']) && strlen($_POST['message'])> 2) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message']; //line 5 $message now set
    $formcontent="From: $name \n Message: $message";
    $recipient = "myemail";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!";

 } else {
    echo 'Your message must be longer than 2 characters!';
 }
?>
于 2013-08-09T00:42:20.303 回答
1

此方法使用if message is less than 2 characters, 然后不处理mail()函数的执行。如果消息不止于此,那么它将继续执行该mail()功能。

<?php 

if (strlen($_POST['message']) < 2) {
die("Sorry, try again.");
}

else {

$name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "myemail";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!";

}

?>
于 2013-08-09T00:52:14.683 回答
0
<?php 
    $message = $_POST['message'];
    if (strlen($message)> 2) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $formcontent="From: $name \n Message: $message";
        $recipient = "myemail";
        $subject = "Contact Form";
        $mailheader = "From: $email \r\n";
        mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
        echo "Thank You!";
    }    


?>
于 2013-08-09T00:41:58.243 回答
0

尝试这个

if (strlen($_POST['$message'])> 2) {
   ' do something;
}
于 2013-08-09T00:45:55.163 回答