Bogdan Laza
Bogdan Laza

Reputation: 67

PHP After submit check if Data input was sent, and if its less than 3 characters give error

How can i check if this form input was sent to this PHP page and if it's longer than 3 characters? If it's less than 3 characters, I want to print an error.

Form

<form action="index.php" method="post" id="formFlow">
    <label for="name" >Name:</label>
    <input type="text"  name="name" id="name" class="border"/>
</form>

PHP

<?php
$var = $_POST["name"];
if (!empty($var) || strlen($var >= 3)) {
    echo "Yes, name is sent";    
}else{  
    echo "Error, name short";
}
?>

Upvotes: 1

Views: 588

Answers (2)

Sazzadur Rahman
Sazzadur Rahman

Reputation: 2930

I think this will solve your problem

<?php
$var = isset($_POST["name"]) ? $_POST["name"] : "";
if (!empty($var) && strlen($var) >= 3) {
    echo "Yes, name is sent";    
} else {  
    echo "Error, name short";
}
?>

Upvotes: 0

devpro
devpro

Reputation: 16117

First of all you can not use OR operator in your condition becuase it will always true either length of string less than 3 or greater than.

You must need to use && operator.

Second you have an issue in strlen() it must need a string not condition.

Example:

if (!empty($var) && strlen($var) >= 3) {

Upvotes: 1

Related Questions