It looks like you're new here. If you want to get involved, click one of these buttons!
var xmlhttp;
function updateDiv(str) {
xmlhttp=GetXmlHttpObject();
if(xmlhttp==null)
{
alert ("Your browser does not support HTTP Request");
return;
}
var url="url.php";
url=url+"?var="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged () {
if (xmlhttp.readyState==4)
{
document.getElementById("some-div").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;
}
Comments
function updataDiv(str) {
new Ajax.Request("url.php",
{
method: 'post',
postBody: '?uid='+str+'&sid='+Math.random();,
onComplete: showResponse
});
}
function showResponse(req){
$('some-div').innerHTML= req.responseText;
}
I figured using straight javascript and xmlhttprequest would be easier to debug.
BTW, I was able to put an alert in the statechanged function before the document update and it worked every time with the alert window popping up. With the alert after the document update it would never work. I can't leave alerts in for general usage.
Also, you clearly have no idea what you are doing. new Ajax.Request ??? try $.ajax();
The documentation is your friend.
function sendRequest(str) {
$.get("url.php", { uid: str, sid: Math.random(); },
showResponse(data){
alert("Data Loaded: " + data);
});
}
function showResponse(req){
$('some-div').innerHTML= req.responseText;
}
Using this it never works. Ideas?
window.onDomReady = sendRequest({$ir['userid']});
I call the function for SendRequest as soon as the DOM is ready. ShowResponse is called by the get function after the data is returned.
After looking closer at the jquery docs i tried to use the more elaborate ajax command:
function sendRequest(str) {
alert ( "data is " + str);
$.ajax({
type: "GET",
url: "url.php",
data: "uid=" + str ,
dataType: "script",
success: showResponse(msg){
alert( "Data Saved: " + msg );
}
});
}
function showResponse(msg){
document.getElementById("some-div").innerHTML= msg.responseText;
}
This worked the very first time and then not again.
$(document).ready(function(){
showResponse(sendRequest('string'));
});
Also, something could be wrong with the server-side script that you are ajaxing.
What has me stumped is why it only works sometimes instead of all the time. When I added in the alerts to the non jquery code it always worked and walked me through each state change. When I removed the alerts it only worked some of the time.
I got the original $.get jquery code to work the first time and then it ceased to work at all after that.
When I use this:
function stateChanged () {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
alert ('Document waiting: xmlhttp request state changed to' + xmlhttp.readyState + ' and xmlhttp.status is ' + xmlhttp.status + '.' );
{
alert ('Document ready: xmlhttp request state changed to' + xmlhttp.readyState + ' and xmlhttp.status is ' + xmlhttp.status + '.' );
document.getElementById("some-div").innerHTML=xmlhttp.responseText;
}
}
It always works but it also steps me through each state change. When I remove the alerts it only works some of the time. I'm new to javascript so I don't really now where to go from here.