General Question

stephen's avatar

Ajax beginner,need help!

Asked by stephen (351points) January 1st, 2009

———————
ajax.html
———————
<script type=“text/javascript”>
function createXHR(){
var xmlHtttpObj;
if(window.XMLHttpRequest){
xmlHtttpObj=new XMLHttpRequest();
}else{
try{
xmlHtttpObj=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e){
try{
xmlHtttpObj=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e){
xmlHtttpObj=false;
}
}
}
return xmlHtttpObj;
}
var xmlHttp=null;
function sendRequest(){
if(!xmlHttp) xmlHttp=createXHR();
if(!xmlHttp) return;
var url=’./handle.php?x=e’;
xmlHttp.open(‘GET’,url,true);
xmlHttp.onreadystatechange=handleBack;
xmlHttp.send(null);

}

function handleBack(){
/* alert(‘handleback’);
if(!xmlHttp) alert(‘bad reqeuest’);
else{
alert(‘good request’);
alert(xmlHttp.readyState);
alert(xmlHttp.status); //always alert 0 ????
}*/alert(xmlHttp.status);
if(xmlHttp.readyState==4){

alert(xmlHttp.responseText);//alert:<?php echo ‘test’; ?>,??? check the pic:
}
}
</script>
</head>

<body>
<a href=”##” onclick=“sendRequest()”>send request</a>
</body>

———————
handle.php:
———————
<?php
echo ‘test’;
?>

Observing members: 0 Composing members: 0

2 Answers

jrpowell's avatar

Your page is “ajax.html”... It isn’t being run through the PHP interpreter. Rename it “ajax.php” and see what happens.

This is assuming you are running a script on a PHP enabled server in the proper way. You need to load the file through the sever.

What is below is assuming you have some knowledge of how PHP works.. If not I would suggest reading this.

This won’t work:
file:///Applications/MAMP/htdocs/login/index.php

This will:
http://localhost:8888/login/index.php

stephen's avatar

thanx @johnpowell,

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther