如何在表单验证中使用数据库数据?

时间:2015-06-02 10:11:53

标签: php mysql validation

您好我有一个使用数据库进行验证的表单,现在验证只是为最新的数据库数据工作,我应该怎么做才能验证每个数据。

数据库信息 http://s1.upload7.ir/downloads/5FzySgwq3DghG9Yw38xa9h773Hsozxst/db.jpg

演示form.php的

<form name="login1" action="demo-login.php" onsubmit="return validateForm1()" method="post" id="form1">
<p>user name: <input type="text" name="luser" id="luser"/></p>
<p>password: <input type="password" name="lpass" id="lpass" /></p>
<input type="submit" value="login" name="submit"/>
</form>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";

$conn2 = new mysqli($servername, $username, $password, $dbname);

if ($conn2->connect_error) {
     die("Connection failed: " . $conn2->connect_error);
} 


$sql2 = "SELECT * FROM users";
$result2 = $conn2->query($sql2);

if ($result2->num_rows > 0) {

     while($row = $result2->fetch_assoc()) {
         $luser = $row["user"];
         $lpass = $row["pass"];
     }
} else {
echo "err_2";
}

$conn2->close();
?> 

<script>
function validateForm1(){
var luser = document.getElementById("luser").value;
var lpass = document.getElementById("lpass").value;
        if (luser == "" || lpass == ""){
            document.getElementById("luser").placeholder="Fill, it's necessary";
            document.getElementById("lpass").placeholder="Fill, it's necessary";
            return false;
        }
        if (luser !== "<?php echo $luser; ?>" || lpass !== "<?php echo $lpass; ?>"){
            alert("user or pass is not true");
            return false;
        }
}
</script>

2 个答案:

答案 0 :(得分:2)

您好像多次使用ID“luser”和“lpass”。 您无法多次分配标记ID。使用类代替并选择那些。

请参阅getElementsByClassName

答案 1 :(得分:1)

我找到了使用php ajax的答案。我希望它对其他人有帮助。

<强>演示form.php的

<form name="login1" action="demo-login.php" onsubmit="return validateForm1()" method="post" id="form1">
<p>user name: <input type="text" name="luser" id="luser" onmouseleave="showUser(this.value)" onchange="showUser(this.value)"/></p>
<p>password: <input type="password" name="lpass" id="lpass" /></p>
<input type="submit" value="login" name="submit"/>
</form>
<div id="passh"></div>


<script>
function validateForm1(){
var luser = document.getElementById("luser").value;
var lpass = document.getElementById("lpass").value;
    if (luser == "" || lpass == ""){
        document.getElementById("luser").placeholder="Fill, it's necessary";
        document.getElementById("lpass").placeholder="Fill, it's necessary";
        return false;
    }
    if (lpass !== document.getElementById("passh").value){
        alert("user or pass is not true");
        return false;
    }
}

function showUser(str) {
    if (str == "") {
        document.getElementById("lpass").placeholder="";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    document.getElementById("passh").value = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","loginpass.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

<强> loginpass.php

<?php
$q = $_GET['q'];

$con = mysqli_connect('localhost','root','','users');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM users WHERE user = '". $q ."' ";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
    echo  $row['pass'] ;
}

mysqli_close($con);
?>