簡體   English   中英

如何在表單旁邊顯示php表單驗證錯誤?

[英]How to display php form validation errors beside the form?

我創建了一個注冊表單,該表單可以通過javascript和php進行驗證。每當發生任何javascript表單驗證錯誤(例如,用戶名是必需的)時,該表單就會顯示在表單旁邊,但是當發生任何php驗證錯誤(例如,用戶名已經存在)時,它是顯示在另一個鏈接上。如何在表格旁邊顯示php驗證錯誤?

<?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.');
    }
     } 

您必須使用ajax來執行php函數,而無需刷新頁面。 讀這個

如果您不想使用ajax,則可以選擇另一種選擇。 可以用php完成,但用戶需要重新加載頁面才能看到錯誤。

為此,必須將html表單代碼和php驗證代碼放在同一文件中。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM