drfu80954 2014-07-06 09:23
浏览 22

在使用PHP验证表单时,我无法显示错误

Im trying to validate a single textbox on the same page using PHP but am unable to get the errors to show. When i click submit and there is nothing in the textbox it works by not letting it go to the next page but it does not show the errors. Here is my code.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')    {
    if ($_POST['IGN'] == ""){
        $errors="Please enter a IGN";   
    }
    if (isset($errors)){
        echo $errors;   
    }
}
?>

<form method="post" action="Queston1.php">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>

I have tried it with and without the if (isset($errors)) to see if that was the problem but both times i get the same result.

Can anyone see or know how to fix this?

  • 写回答

2条回答 默认 最新

  • dsmlf1207915 2014-07-06 09:31
    关注

    You should use jQuery or Javascript to validate before submitting the form.

    But instead of

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    

    Try

    $errors = '';
    if(isset($_POST['IGN'])){
        if($_POST['IGN'] == ''){
            $errors .= 'Please Enter a IGN';
        }
    }
    if($errors!=''){
        echo $errors;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致