I want to get the current value of the input field, then echo some information from a database based on the fields value, while not using a submit.
Something like when you are filling out a new profile for a site and instead of going through the hassle of clicking submit and going back if your username is already taken, to display an error message next to that field.
This is what I've got so far:
<html>
<body>
<form name='Form'>
Date: <input type='text' id='sheetid' />
<input type='button' onclick='ajaxFunction()' value='Query!' /> <br />
</form>
<div id='content'>Result</div>
</body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("ERROR!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('content');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var sheetid = document.getElementById('sheetid').value;
var queryString = "?url=" + sheetid;
ajaxRequest.open("GET", "test.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
</html>
and my php:
<?
$st=$_POST['sheetid'];
require_once('database_connect.php');
$inf = "SELECT * FROM `comments` WHERE date='$st' ORDER BY time ASC";
$info = mysql_query($inf);
if(!$info) die(mysql_error());
$info_rows = mysql_num_rows($info);
if($info_rows > 0) {
echo '<table width="95%">';
while($info2 = mysql_fetch_object($info)) {
echo '<tr>';
echo '<td>';echo '<a href="mailto:'.$info2->username.'@mail.com">'.stripslashes($info2->username).'</a>'; echo "("; echo date('H:i', $info2->time)."): "; echo stripslashes($info2->comment).'</td>'.'</tr>';
}
}
?>
I'm not sure why this doesn't display any comments and also can't figure out how to make it work without pressing the query button, but after the user has finished typing or clicked out of the input box.
Any feedback would be appreciated.
Thnaks