2013-09-29 60 views
0

我已经创建了一个注册表单,通过javascript和php验证。每当任何javascript表单验证错误发生时,如用户名是必需的,它会显示在表单旁边,但是当任何php验证错误发生时,如用户名已存在,它显示在另一个链接上。如何在窗体旁显示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.'); 
    } 
    } 
+0

您正在使用两层建筑还是三层建筑? – Gourav

+1

你是什么意思由另一个链接?,并张贴一些代码片段,以获得更好的答复。 – Jinandra

+0

@sanki我发布了代码 –

回答

0

另一个选项,如果你不想使用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> 
0

您必须使用ajax来执行php函数而不刷新页面。 Read this