2015-05-22 94 views
0

问题是我想发送ajax数组到一个php,我可以使用它进行不同的处理,实际上我的html页面中有很多文本字段,而我有分配给他们的名字我想通过ajax(仅限于JavaScript)将这个名称数组发送到我的php页面,我将接收这个数组并将其用于不同的目的。如何在ajax请求中发送数组js数组到php页面

这里是我的示例HTML代码:

<input type="text" name="top_phone_cal2" class="form-control" > 
<input type="text" name="top_phone_cal2" class="form-control" > 
<input type="button" onclick="dis_phone_call2_top()" class="form-control" > 

JavaScript代码:

function dis_phone_call2_top() 
    { 
    var x =document.getElementsByName('top_phone_cal2'); 
    var top_phone_cal2 = [];  
    for(i=0;i<x.length;i++) 
    { 
     top_phone_cal2[i]=x[i].value; 
     //alert("cars value: "+top_phone_cal2[i]+" loop value: "+x.length); 
    } 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() 
    { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
     var bbb=xmlhttp.responseText;alert(bbb);         
     } 
    }  
    xmlhttp.open("GET", "intervention_phonecal2_insrt.php?value="+top_phone_cal2, true); 
    xmlhttp.send(); 
    } 

这里是PHP代码:

<?php 
    $top_phone_cal2= $_REQUEST["value"];   
    echo $top_phone_cal2; 
?> 

在这段代码的问题是,通过这个代码我无法接收数组在php页面而不是这个我收到变量在PHP页面 例如:
如果在第一个文本字段我进入John并在第二个文本字段我进入lincon然后在页面它显示像这样johnlincon但我要显示我喜欢这个echo $top_phone_cal2[0] =约翰和echo $top_phone_cal2[1] =林肯 请你告诉我这个怎么做?

+0

使用JSON.stringify()将数组的json生成并将其作为查询字符串发送。 – Rayon

+0

如何使用它我从来没有做过它? –

+1

没有什么像'ajax数组'。了解一般HTTP协议的工作原理,然后了解您的需求的特定实现。 – moonwave99

回答

1

试试这个:

<input type="text" name="top_phone_cal2[]" class="form-control" /> 
<input type="text" name="top_phone_cal2[]" class="form-control" /> 

我希望它能帮助。

0

使用此代码:

function dis_phone_call2_top() 
{ 
    var x =document.getElementsByName('top_phone_cal2'); 
    var top_phone_cal2 = []; 
    for(i=0;i<x.length;i++) 
    { 
    top_phone_cal2[i]= x[i].value; 
    //alert("cars value: "+top_phone_cal2[i]+" loop value: "+x.length); 
    } 
    var value_str = 'value[]=' + top_phone_cal2.join('&value[]=') 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() 
    { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
    { 
     var bbb=xmlhttp.responseText;alert(bbb); 
    } 
    } 
    xmlhttp.open("GET", "intervention_phonecal2_insrt.php?"+value_str, true); 
    xmlhttp.send(); 
} 

解释:你应该张贴的价值的正确格式GET PARAMS。 PHP将此值收集到$_GET['value']变量。