xmlhttp和PHP无法正常工作?

时间:2013-10-28 09:57:56

标签: php xmlhttprequest

所以我一直在学习使用xmlhttp而我无法使用这个简单的脚本:

<!DOCTYPE html>
<html>
    <head>

        <script>
            function print_stuff(){
                document.getElementById("two").innerHTML="working";
                if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                }
                else{// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function(){
                  if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("one").innerHTML=xmlhttp.responseText;
                  }
                }
                xmlhttp.open("POST","index.php",true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");   
                xmlhttp.send("email=" + document.getElementByName("email").value + "&name="+ document.getElementByName("name").value);
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
    </head>
    <body>
            Name: <input type="text" name="name"/></br>
            Email:<input type="text" name="email"/></br>
            <button onclick="print_stuff()">Button</button></br>
        <span id="one"></span>
        <div id="two"></div>
    </body>
</html>

还有index.php:

    <?php 
    $name = $_POST["name"];
    $email = $_POST["email"];
    echo "Name: ",$name,"</br> Email: ", $email;
    ?>

这背后的想法非常简单:您可以获得用户的姓名和电子邮件,并使用“POST”方法将其打印出来。我有一种感觉,这是一个非常简单的错误,虽然我找不到它......任何帮助都赞赏!

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
    </head>
    <body>
            Name: <input type="text" name="name" id="name"/></br>
            Email:<input type="text" name="email" id="email"/></br>
            <button onclick="print_stuff()">Button</button></br>
        <span id="one"></span>
        <div id="two"></div>
    </body>
</html>

<script>
    function print_stuff(){

       var email = document.getElementById("email").value;
       var name = document.getElementById("name").value;

       if (window.XMLHttpRequest)
        {
            var xmlhttp=new XMLHttpRequest();
        }
        else
        {
            var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            { 
                array = new Array(4);
                array = xmlhttp.responseText.split("##"); //slicing string into array to get separate result

                document.getElementById("one").innerHTML = array.slice(0,1);
                document.getElementById("two").innerHTML = array.slice(1,2);
            }
        }
        xmlhttp.open("GET","index.php?email="+email+"&name="+name,true);
        xmlhttp.send();
    }
</script>

<强>的index.php:

<?php 
    $name = "Name: ".$_GET["name"];
    $email = "Email: ".$_GET["email"];

    echo $name."##".$email;
?>