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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵