doulongsha5478 2013-09-29 01:41
浏览 57
已采纳

如何在表单旁边显示php表单验证错误?

i have created a signup form which is validated by both javascript and php.Whenever any javascript form validation error occur such as username is required,it is displayed beside the form,But when any php validation error occur such as username already exist,it is displayed on another link.How can i display php validation errors beside the form?

<?php

 include('configdb.php');
 if(isset($_POST['submit']))
 {
 checks if the username is in use

if (!get_magic_quotes_gpc()) {

    $_POST['username'] = addslashes($_POST['username']);

}

 $usercheck = $_POST['username'];

 $sql1 = "SELECT username FROM users WHERE username = '$usercheck'"; 
 $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());


//if the name exists it gives an error

if (mysqli_num_rows($result1) != 0) {

    die('Sorry, the username '.$_POST['username'].' is already in use.');
    }
     } 
  • 写回答

2条回答 默认 最新

  • duankan6894 2013-09-29 04:06
    关注

    Another option if you don't want to use ajax. It can be done with php but user need to reload the page to see error.

    To do this, html form code and php validation code must be placed at the same file.

    <?php 
        $error = false;
        if(isset($_POST['submit'])){                
            // your validation
                // if something wrong
                $error = true;  
        }
    ?>
    <form action="validation_checking.php" method="post">
        <?php
            if($error)
            { echo '<p style="color:red;">Sorry, the username '.$_POST['username'].' is already in use.</p>'; } 
        ?>
        <input type="text" name="username"> 
        <input type="submit" value="submit">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)