2013-10-28 58 views
0

所以,我一直在學習使用XMLHTTP,我不能讓這個簡單的腳本工作:xmlhttp和PHP不能正常工作?

<!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」方法打印出來。我有一個意識,這是一個非常簡單的錯誤,雖然我找不到它......任何幫助表示讚賞!

+0

哪一部分無法正常工作?你似乎有所有的零件。 – Halcyon

+0

的innerHTML不會改變。我的意思是,它應該是xmlhttp或PHP腳本的問題,我猜... – eksponente

回答

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; 
?> 
+0

這似乎對我很好,不知道爲什麼它有downvote。 (請留下評論,如果你downvote所以_we_知道什麼是錯的)。 – Halcyon

+0

即使我dnt知道爲什麼some1 downvote到我的答案雖然它的工作... – Ashish