douxian6008
2017-10-13 00:08
浏览 20
已采纳

PHP - 总是在表单中获得错误

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.

图片转代码服务由CSDN问答提供 功能建议

我有这个表格,但我一直得到“你的喊叫中不能少于7个字符。 “错误。 这就是我所拥有的,对我来说似乎很好:

  if(isset($ _ POST ['submit'])){
 $ shout = strip_tags($ _ POST)  ['shout']); 
 $ value =(!empty($ shout))?  'value =“'。$ shout。'”':''; 
 
 if if(empty($ shout)){
 $ response [] ='&lt; div class =“alert alert-danger”&gt;  &LT;强&GT;!错误&LT; /强&GT; 你的喊声不能为空。&lt; / div&gt;'; 
} elseif($ shout&gt; 50){
 $ response [] ='&lt; div class =“alert alert-danger”&gt;&lt; strong&gt; 错误&LT;!/强&GT; 你不能在喊叫中使用超过50个字符。&lt; / div&gt;'; 
} elseif($ shout&lt; 7){
 $ response [] ='&lt; div class =“alert alert-danger”&gt  ;&LT;强&GT;错误&LT;!/强&GT; 你的喊叫不能少于7个字符。&lt; / div&gt;'; 
} else {
 $ response [] ='&lt; div class =“alert alert-success”&gt;&lt; strong&gt;成功!  &LT; /强&GT; 你的喊声已经发布并放在了shoutbar中。&lt; / div&gt;'; 
} 
} 
 
 if if(isset($ response)){
 foreach($ response as $ respons){
  echo $ respons; 
} 
} 
?&gt; 
&lt; form method =“post”&gt; 
&lt; table class =“table”&gt; 
&lt; tr&gt; 
&lt; td&gt;  &lt; strong&gt;您的留言&lt; / strong&gt;&lt; / td&gt; 
&lt; td&gt;&lt; input type =“text”name =“shout”placeholder =“输入您的喊声”&lt;?php echo $ value;  ?&GT;  class =“form-control”&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;&lt; / td&gt; 
&lt; td&gt;&lt; input type =“ 提交“value =”发布“name =”提交“class =”btn btn-default“&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; / table&gt; 
&lt; / form&gt; 
    
 
 

为什么我一直收到此错误? 我键入的输入介于7到50个字符之间,因此它应该采用 else 路径。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxin8383 2017-10-13 00:13
    已采纳

    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.

    打赏 评论
  • dongyongmin5711 2017-10-13 01:00

    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

    打赏 评论

相关推荐 更多相似问题