onchange函数期间的ajax


ajax during onchange function

我有一个非常简单的案例,但让我感到困惑,这里是script=>

<!DOCTYPE html>
<html>
<head>
    <title>HI</title>
    <link rel="stylesheet" href="./scripts/css/default.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form name="ok" method="post" action="index.php">
    <select name="sel" id="sel"><option value="a">1</option><option value="b">2</option><option value="c">3</option></select>
</form>
FIRST PAGE

<div id="as"></div>
<script type="text/javascript">
document.getElementById("sel").onchange = function(){
    var doc = false;
    if (window.XMLHttpRequest){
        doc = new XMLHttpRequest();
    }
    if(doc){
        doc.open("POST","./index.php",true);
        doc.onreadystatechange = function(){
            if (doc.status==200 && doc.readyState==4){
                document.getElementById("as").innerHTML = doc.responseText;
            }
        };
        doc.send("sel=" + document.getElementById("sel").value);
    }
};
</script>
<?php
if (isset($_POST['sel'])){
    echo $_POST['sel'];
}
?>
</body>
</html>

从这个脚本中,我希望onselect更改返回select元素的值,但它再次返回整个页面,为什么?有什么想法吗?请帮忙,谢谢:)

PS。这是self-index.php页面

创建一个单独的php文件来处理ajax请求,这样ajax请求只会显示php文件所响应的文本。

这是我的示例代码:

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
          if (isset($_POST['sel'])){
             echo "yeah";
          }
  }else{die('You are not allowed to access this page!');}