0

所以我一直在学习使用 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”方法将其打印出来。我有一种感觉,这是一个非常简单的错误,虽然我找不到它......任何帮助表示赞赏!

4

1 回答 1

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>

索引.php:

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

    echo $name."##".$email;
?>
于 2013-10-28T10:14:23.177 回答