Ajax发布到PHP并获取返回数据

时间:2014-12-11 14:35:51

标签: javascript php ajax

我正在开发一个滑块来设置我的预算,并希望达到我在slider1.php中设置的值。但是,当我尝试使用下面的代码时,我遇到了一个错误 “注意:未定义的索引:第4行的C:\ xampp \ htdocs \ 1204763e \ slider1 \ slider1.php中的slideStatus 谢谢,PHP文件说“

在slide.php中,我插入了这组代码:

     <html>
    <head>
    <script language="JavaScript" type="text/javascript">

    function ajax_post(val){

        var hr = new XMLHttpRequest();

        var url = "slider1.php";
        var ss = document.getElementById('sliderStatus').innerHTML = val;
        var vars = "sliderStatus="+ss;
        hr.open("POST", url, true);

        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("status").innerHTML = return_data;
            }
        }
        // Send the data to PHP now... and wait for response to update the status div
        hr.send(vars); // Actually execute the request
        document.getElementById("status").innerHTML = "processing...";
    }
    </script>
    </head>
    <body>
    <h2>Ajax Post to PHP and Get Return Data</h2>

    <input type="range" name="slide" min="0" max="100" value="50" step="2" onChange="ajax_post(this.value)" />
    <br /><br />
    <span id="sliderStatus">50</span>
    <br/><br/>

    <div id="status"></div>

    </body>
    </html>



In slider1.php, I inserted this set of code:

    <?php 
    echo 'Thank you '. $_GET['slideStatus'] . ', says the PHP file';
    ?>

1 个答案:

答案 0 :(得分:3)

您似乎尝试访问&#34; slideStatus&#34;但是你要发布&#34; sliderStatus&#34;。