Click to See Complete Forum and Search --> : My first AJAX woes


blazes816
08-23-2006, 04:49 PM
Hey. I told you I was done with the stupid quetions, but I guess I lied. I'm learning ajax and have this simple script:

var xmlHttp;
function createRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function createpost(){
createRequest();
var url = "preview.php";
var query = document.getElementById("letterbody");
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = StateChange;
xmlHttp.send(query);
}
function StateChange(){
if(xmlHttp.readyState == 4){
alert(xmlHttp.request.ResponseText);
}}


So I hit the preview button, and it links to createpost(); But the whole page reloads and the response isn't alerted. Here's preview.php:

<?php
print 'bleh!';
?>

Whoknow
08-23-2006, 08:34 PM
try this

function StateChange(){
if(xmlHttp.readyState == 4){
alert(xmlHttp.responseText);
}

blazes816
08-23-2006, 09:17 PM
Okay thanks. That worked if I call the function in the JS, but if I call it from the submit button, it brings up to blank alerts.

EDIT: Doesn't work at all in IE

Whoknow
08-23-2006, 09:43 PM
I don't know what's going behind the scene , but I use your js and .net for server side , it works fine for both normal button click and submit button

blazes816
08-23-2006, 10:01 PM
I don't know. I'll fiddle with it. I just got an ajax book tonight so I might figure it out. Thanks for the help.