0
raise.js:

window.onload=init;
function init(){
    var submit=document.getElementById("submit");
    submit.onclick=sub;
}

function sub(){
    var url="raise.php";
    var title=document.getElementById("title");//title of a question
    var content=document.getElementById("inputContent");//content of a question

    var checktype=document.getElementsByName("type");
    var type;//type of a question
    if(checktype[0].checked){
        type="java";
    }
    else if(checktype[1].checked){
        type="c++";
    }
    else{
        type="html";
    }

    var point=document.getElementsByTagName("select");
    var reward=point[0].value;//reward point

    url=url+"?title="+title.value;
    url=url+"?content="+content.value;
    url=url+"?type="+type;
    url=url+"?reward="+reward;

    var xmlhttp;
    if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    }
    else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4&&xmlhttp.status==200){
        alert("asde");
    }
    }

    xmlhttp.open("GET",url,true);
    xmlhttp.setRequestHeader("Content-Type","utf-8");
    xmlhttp.send();

}

raise.php:

<?php
  echo "<script>alert('123')</script>";
 $title=$_GET['title'];
 $content=$_GET['content'];
 $reward=$_GET['reward'];
 $type=$_GET['type'];
?>

that confuses me alert('123') is not executed but alert("asde") is ,I want to know why..and am I right while trying to retrieve these data with php...I'm not very familiar with ajax...please show me some codes based on my data..thank you very much..

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
Peter Guan
  • 308
  • 3
  • 16
  • The alert is activated out of your context so you don't see it. You will probably be able to see the file output in using _inspect element_ – raam86 Dec 04 '13 at 15:56
  • There are literally billions of records about such topic, you can read them [even synchronously]. – moonwave99 Dec 04 '13 at 15:57
  • does it mean I can retrieve data like this $title=$_GET['title']? thanks – Peter Guan Dec 04 '13 at 15:58

3 Answers3

0

A script element won't do anything unless it is part of a rendered HTML document.

The server returns it to the browser, then it sits in xmlhttp.responseText where you are ignoring it.

(That said, even if you didn't ignore it, you might still have problems).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You simply not attaching the response body (the JavaScript) to your DOM, try:

// ...
xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4&&xmlhttp.status==200){
        alert("asde");
        alert(xmlHttp.responseText);
        // here you have to attach xmlHttp.responseText to your DOM
    }
}
// ...

This should alert you "asde" and after that "script>alert('123')/script>"

NiMeDia
  • 995
  • 1
  • 15
  • 27
0

You better use Jquery with Ajax to POST or GET data instead of only Ajax. You can have a look here jquery ajax. Here you will find out where alert should be used.

Hasib Mahmud
  • 806
  • 1
  • 10
  • 29