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条)

报告相同问题?

悬赏问题

  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))