We have a form to get user name input and have a button associated with function getResult().
getResult()
function getResult() { var xmlhttp = new XMLHttpRequest(); var name = document.getElementById("name").value; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { setTimeout(function() {document.getElementById("result").innerHTML = xmlhttp.responseText;}, 1000); } else { document.getElementById("result").innerHTML = "Loading..."; } } xmlhttp.open("POST", "getuserhash.php"); xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.send("name=" + name); }
getuserhash.php
include_once('connection.php'); $name = $_POST['name']; $query = $pdo->prepare("SELECT user_password FROM user WHERE user_name = ?"); $query->bindValue(1, $name); $query->execute(); $data = $query->fetch(); echo $data["user_password"];
It works!
Use jQuery to rewrite getResult()
function getResult() { var name = $("#name").val(); $.post("getuserhash.php", {name:name}, function(data) { $("#result").html(data); } ); }
No comments:
Post a Comment