[英]How to send array js array to php page in ajax request
The problem is I want to send ajax array to a php where I can use it for different processing, actually I have a lot of text fields in my html page and I have assign them name I want to send this name array to my php page through ajax(JavaScript only) where I will receive this array and will use it for different purposes. 问题是我想将ajax数组发送到php,在其中我可以使用它进行不同的处理,实际上我的html页面中有很多文本字段,并且已经为其指定了名称,我想将此名称数组发送到我的php页面通过ajax(仅JavaScript),我将在其中接收此数组并将其用于不同的目的。
Here is my example HTML code: 这是我的示例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 code: 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();
}
Here is php code: 这是PHP代码:
<?php
$top_phone_cal2= $_REQUEST["value"];
echo $top_phone_cal2;
?>
The problem in this code is that through this code I am unable to receive array in php page instead of this I receive the variable in php page For Example: 此代码中的问题是,通过此代码,我无法在php页面中接收数组,而无法在php页面中接收变量。例如:
if in first text field I entered John
and in second text field I entered lincon
then in page it display it like this johnlincon
but I want to display I like this echo $top_phone_cal2[0]
=john and echo $top_phone_cal2[1]
=lincon kindly tell me how to do this? 如果在第一个文本字段中输入了John
,在第二个文本字段中输入了lincon
则在页面中,它像johnlincon
一样显示,但我想显示出我喜欢这个echo $top_phone_cal2[0]
= john和echo $top_phone_cal2[1]
= lincon请告诉我该怎么做?
Try this: 尝试这个:
<input type="text" name="top_phone_cal2[]" class="form-control" />
<input type="text" name="top_phone_cal2[]" class="form-control" />
I hope it helps. 希望对您有所帮助。
Use this code: 使用此代码:
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();
}
Explanation: you should post the proper format of value in GET params. 说明:您应该在GET参数中发布正确的值格式。 PHP collect this value to $_GET['value']
variable. PHP将此值收集到$_GET['value']
变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.