2012-06-30 91 views
0

如何使用ajax将循环数据从html页面发送到php页面。我很努力,但我没有办法将循环数据传递到php页面。如何通过AJAX将循环数据从html页面传递到php页面

order.html页代码在下文描述:

<html> 
<body> 

<script language="javascript" type="text/javascript"> 
<!-- 
//Browser Support Code 
function ajaxFunction(){ 
    var ajaxRequest; // The variable that makes Ajax possible! 

    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
    } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    // Create a function that will receive data sent from the server 
    ajaxRequest.onreadystatechange = function(){ 
     if(ajaxRequest.readyState == 4){ 
      var ajaxDisplay = document.getElementById('ajaxDiv'); 
      ajaxDisplay.innerHTML = ajaxRequest.responseText; 
     } 
    } 
    var age = document.getElementById('age[]').value; 
    var queryString = "?age=" + age ; 
    ajaxRequest.open("GET", "example.php" + queryString, true); 
    ajaxRequest.send(null); 
} 

//--> 
</script> 



<form name='myForm'> 
Name: <input type='text' id='age[]' Name='age[]' /> <br /> 
Name: <input type='text' id='age[]' Name='age[]'/> 
<br /> 
<input type='button' onclick='ajaxFunction()' value='Query MySQL' /> 
</form> 
<div id='ajaxDiv'>result will display here</div> 
</body> 
</html> 

使用example.php页代码在下文描述:

<?php 

$len = count($_GET['age']); 

for($x=0;$x<$len;$x++) 
{ 

echo $service[$x]=$_GET['age'][$x]; 

} 

?> 
+0

什么是循环数据? – MDrollette

+0

我已经删除了'jQuery'标签,因为问题与jQuery无关。 – undefined

回答

0

的[]应追加到查询字符串,而不是输入名称

var queryString = "?age[]=" + age1 + "&age[]=" + age2 + "&age[]=" + age3 ; 
ajaxRequest.open("GET", "example.php" + queryString, true); 
+0

修复语法并添加例子,现在希望更多的“评论”:) –

+0

好吧,祝你好运:) – undefined

相关问题