Php Ajax Error: Can't get data from database

I have a problem with my Ajax php coding.
The problem is when the user want to login and enter their password in input field ,the live checking function output only return blank value in select box field, not the data from database.
Below is my code:

Login.php


username:<input type="text" class="name" id="uname" placeholder="enter your username here"><br>
password:<input type="password" class="pass" id="pword" placeholder="password here"><br>
<div id="txt">
   responsibility:<select id="opti">
        <option value=""> Select your responsibility</option>
</select><br></div>


<script>
function showUser(str) {
   if (str == "") {
    document.getElementById("txt").innerHTML = "";
    return;
} else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txt").innerHTML = this.responseText;
        }
    };
    xmlhttp.open("GET","dblogin.php?q="+str,true);
    xmlhttp.send();
   }
}
document.getElementById('pword').addEventListener("keyup", function(str) {
    showUser(str);
}, false);
</script>

DbLogin.php

<?
$q=intval($_GET["pword"]);

require ("config.php");
$link=mysqli_connect($host,$user,$pass,$db);

$query="select responsibility from testing where password = '".$q."'";
$result=mysqli_query($link,$query);
    if($result){
?>
<form>
responsibility:<select id="opti">
<?
    while($row=mysqli_fetch_array($result))
    echo"<option value='$row[1]'>$row[1]</option>";
?>
</select>
<input type=submit value ="submit">
<?
}
else
{
?>
Error:No Data Found
<?
}
?>

config.php

<?
   $host="localhost";
   $db="logindb"
   $user="root"
   $pass="";
?>

the ajax function is work, but the database value not shown. Can anybody tell me what wrong with my coding and also how to make onkeyup function run when user finish typing? below is the example:

Is it finding the entry in the database correctly? I have only done a very small amount of Ajax / JS, but this doesn’t seem correct to me:

xmlhttp.open("GET","dblogin.php?q="+str,true);

and then

$q=intval($_GET["pword"]);

While you check that the query has executed, you don’t seem to check whether it returned any results before drawing the select box. Again, not that familiar with mysqli (I use PDO) but I’d have thought that as long as there’s no syntax error the mysqli_query() would return a value that would allow the select to be drawn, even if it finds no values.

And guessing for a third time, I’d imagine the keyup runs every time the user releases a key, as it has no way to know whether that’s the last thing they’ll type in the box it will call your lookup every time they type something. That’s probably fine as long as the connection to the server, and the speed at which it executes the query, is very fast. Once it gets to the speed I get when google does the same instead of letting me finish typing, it will become quite irritating.

Is it correct that your passwords are always integer numeric values? That seems quite limiting in terms of security, to me.

thank for replying. about the password, this only for testing purpose. so for the real thing, i will change it later.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.