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

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP