0

私はそのような状況にあります。以下に示すようなselectフォームがあり、ajax関数を呼び出して、select formの変更に依存する結果を取得したいが、ajaxからページ全体を取得したい

これがスクリプトです

<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 method="post">
<select name="s" id="s">
    <option value="one">A</option>
    <option value="two">B</option>
    <option value="three">C</option>
</select>
</form>

Hello guys ...
<div id="xe"></div>

<script type="text/javascript">
document.getElementById("s").onchange = function(){
    var h = false;
    if (window.XMLHttpRequest){
        h = new XMLHttpRequest();
    }
    if (h){
        h.open("POST","index.php",true);
        h.onreadystatechange = function(){
            if (h.readyState==4 && h.status==200){
                document.getElementById("xe").innerHTML = h.responseText;
            }
        };
        h.send("s=" + document.getElementById("s").value);
    }
};
</script>

<?php
if (isset($_POST['s'])){
    echo $_POST['s'];
}
?>


</body>
</html>

何か案は ?助けてください、ありがとう...

4

2 に答える 2

1

$ _POST行をファイルの先頭に配置し、その後、exit()を実行する必要があります。

<?php
if (isset($_POST['s'])){
    echo $_POST['s'];
    exit;
}
?>
<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 method="post">
<select name="s" id="s">
    <option value="one">A</option>
    <option value="two">B</option>
    <option value="three">C</option>
</select>
</form>

Hello guys ...
<div id="xe"></div>

<script type="text/javascript">
document.getElementById("s").onchange = function(){
    var h = false;
    if (window.XMLHttpRequest){
        h = new XMLHttpRequest();
    }
    if (h){
        h.open("POST","index.php",true);
        h.onreadystatechange = function(){
            if (h.readyState==4 && h.status==200){
                document.getElementById("xe").innerHTML = h.responseText;
            }
        };
        h.send("s=" + document.getElementById("s").value);
    }
};
</script>


</body>
</html>
于 2012-07-22T22:44:58.937 に答える
1

実際、index.phpはページ全体を返し、そのすべてのHTMLが埋め込まれています。

partial.phpのを作成して、ブラウザのページに挿入する必要のある部分を返し、AJAXリクエストをその部分に向けることができます。

于 2012-07-22T19:15:04.997 に答える