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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路