Reputation: 183
I have this form but I keep getting the "You cannot use less than 7 characters in your shout."
error. This is what I have, and to me it seems fine:
if(isset($_POST['submit'])) {
$shout = strip_tags($_POST['shout']);
$value = (!empty($shout)) ? 'value="' . $shout . '"' : '';
if(empty($shout)) {
$response[] = '<div class="alert alert-danger"><strong>Error!</strong> Your shout cannot be empty.</div>';
} elseif($shout > 50) {
$response[] = '<div class="alert alert-danger"><strong>Error!</strong> You cannot use more than 50 characters in your shout.</div>';
} elseif($shout < 7) {
$response[] = '<div class="alert alert-danger"><strong>Error!</strong> You cannot use less than 7 characters in your shout.</div>';
} else {
$response[] = '<div class="alert alert-success"><strong>Success!</strong> Your shout has been posted and placed in the shoutbar.</div>';
}
}
if(isset($response)) {
foreach($response as $respons) {
echo $respons;
}
}
?>
<form method="post">
<table class="table">
<tr>
<td><strong>Your shout</strong></td>
<td><input type="text" name="shout" placeholder="Type your shout" <?php echo $value; ?> class="form-control"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Post it" name="submit" class="btn btn-default"></td>
</tr>
</table>
</form>
Why do I keep getting this error? The input I typed is inbetween 7 and 50 characters so it should go and take the else
path.
Upvotes: 0
Views: 46
Reputation: 568
You need to compare lenghts, using mb_strlen() (multibyte) instead strlen(), available since PHP 4 >= 4.0.6, to avoid fatal errors on string lenghts ending in a non standard char, like latin tilde (á,é,í,ó,ú,ñ) or any other special char.
http://php.net/manual/es/function.mb-strlen.php
Upvotes: 1
Reputation: 7230
You need to strlen($shout)
in your comparison. You're comparing the $shout
as a string and the integer 7
is also being converted to a string for the comparison, but you're intending to compare the integer length to another integer value.
Upvotes: 2