Link
Tank
- Joined
- Apr 28, 2004
- Messages
- 2,446
- Reaction score
- 3
Hey guys. I know this isn't exactly the best forum to ask this, but I figure theres got to be 1 or 2 people here that know their way around a script
I'm starting to learn ajax, and have hit a little bit of a stumbling block. What I am trying to do is enter some text in an input box, and have that update into a <div>
. It does work, however, it only updates when the user clicks out of the box. Ideally I would want to to update as soon as text is entered, or maybe after a few seconds delay. I had thought the "onchange" value would call the update as soon as it is changed, but it dosen't seem to work that way.
Heres the code I'm using: Year.php
The javascript that is called in the head of the page
And finally, the year_update.php that the javascript calls
As I say, it does work, when the user clicks out the box, whatever was typed in the box replaces "temp", but I can't figure out why it waits untill the user clicks out. Any suggestions?
I'm starting to learn ajax, and have hit a little bit of a stumbling block. What I am trying to do is enter some text in an input box, and have that update into a <div>
. It does work, however, it only updates when the user clicks out of the box. Ideally I would want to to update as soon as text is entered, or maybe after a few seconds delay. I had thought the "onchange" value would call the update as soon as it is changed, but it dosen't seem to work that way.
Heres the code I'm using: Year.php
Code:
<div id="yearshowupdate">
Temp
</div>
The javascript that is called in the head of the page
Code:
var xmlhttp
function update_year(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="year_update.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
*************ElementById("yearshowupdate").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
And finally, the year_update.php that the javascript calls
Code:
<?php
echo $_GET['q'];
?>
As I say, it does work, when the user clicks out the box, whatever was typed in the box replaces "temp", but I can't figure out why it waits untill the user clicks out. Any suggestions?