如果长度<不发送消息2

时间:2013-08-09 00:39:36

标签: php javascript html

我正在尝试将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 个答案:

答案 0 :(得分:1)

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

当语句为true时,请注意{ }if语句一起使用花括号之间的内容。

<?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!';
 }
?>

答案 1 :(得分: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!";

}

?>

答案 2 :(得分: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!";
    }    


?>

答案 3 :(得分:0)

试试这个

if (strlen($_POST['$message'])> 2) {
   ' do something;
}