Phil Famme
Phil Famme

Reputation: 119

Don't send message if length < 2

I am trying to add php to my contact us page to make sure the message is at least a few characters long for it to be sent. I have tried if (strlen()> 2) but it is just letting messages go through anyway.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();
    });

Upvotes: 0

Views: 290

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

This method uses the if message is less than 2 characters, then do not process the execution for the mail() function. If the message is more than that, then it will proceed in executing the mail() function.

<?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!";

}

?>

Upvotes: 1

Sir
Sir

Reputation: 8277

Your $message is not set yet so the if statement will fail. Change it to the post message. You also need to check the message is set before you check its string length.

Note also the use of { } with the if statement when the statement is true the content between the curly braces executes.

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

Upvotes: 1

cole71
cole71

Reputation: 5

Try this

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

Upvotes: 0

falinsky
falinsky

Reputation: 7428

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


?>

Upvotes: 0

Related Questions