Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向php发送带有ajax请求的数组_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 向php发送带有ajax请求的数组

Javascript 向php发送带有ajax请求的数组,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我创建了这样的数组[“9”、“ques_5”、“19”、“ques_4”]。现在我想把它从JS发送到PHP,但是我没有得到正确的结果。我的JS代码是: $(".button").click(function(e) { e.preventDefault(); $.ajax({ type : 'post', cache : false, url : 'test/result.php', data

我创建了这样的数组
[“9”、“ques_5”、“19”、“ques_4”]
。现在我想把它从JS发送到PHP,但是我没有得到正确的结果。我的JS代码是:

$(".button").click(function(e) {

    e.preventDefault();
    $.ajax({
        type    : 'post', 
        cache   : false,
        url     : 'test/result.php',
        data    : {result : stuff},
        success: function(resp) {
            alert(resp);
        }
    });
});

在上面的代码
中,stuff
是一个包含记录的数组。我如何用上面的代码发送这个数组,然后在PHP中我想处理这个数组,就像
ques_5
是键,
9
成为该键的值一样。

如果你想发送键值对,这就是我所看到的,最好使用PHP JSON库(像这样的库…)

然后您可以使用JSON格式发送实际的键值对,如。。。 {“问题5”:“19”,“问题4”:“19”}

试试这个

var array = ["9", "ques_5", "19", "ques_4"];
console.log(array.join(","));
上面的代码将输出带有逗号分隔的字符串,如
9、ques_5、19、ques_4
,然后将其粘贴到ajax调用中

然后在
php中分解该字符串

其他可能的解决办法

首先

var obj = { 'item1': 'value1', 'item2': 'value2' };

$.ajax(
{
    type:  'post', 
    cache:  false ,
    url:  'test/result.php',
    data:  { result : JSON.stringify(obj) },
    success: function(resp)
    {
        alert(resp);
    } 
});

var a = $.JSON.encode(obj);

$.ajax(
{
    type:  'post', 
    cache:  false ,
    url:  'test/result.php',
    data:  { result : a },
    success: function(resp)
    {
        alert(resp);
    } 
});

In PHP File

<?php
    $json = $_POST["data"]
    var_dump(json_decode($json));
?>
var a=$.JSON.encode(obj);
$.ajax(
{
键入:“post”,
cache:false,
url:'test/result.php',
数据:{result:a},
成功:功能(resp)
{
警报(resp);
} 
});
在PHP文件中

您可以将json格式的
数组发送到php,然后使用
json\u decode
函数返回数组,如

在ajax调用中,您必须发送json,您需要首先生成值数组,以便以正确的形式获得它 因此,json看起来像
{“ques_5”:“9”,“ques_4”:19}

并在ajax调用中使用

 data: JSON.stringify(`your created json`),
 contentType: "application/json; charset=utf-8",
 dataType: "json",
在PHP中,它看起来像

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
?>

您可以将数据作为对象传递给PHP脚本。假设您的JSON对象如下所示:

var stuff ={'key1':'value1','key2':'value2'};
可以通过两种方式将此对象传递给php代码:

1。将对象作为字符串传递:

AJAX调用:

$.ajax({
    type    : 'POST',
    url     : 'result.php',
    data    : {result:JSON.stringify(stuff)},
    success : function(response) {
        alert(response);
    }    
});
$.ajax({
    type    : 'POST',
    url     : 'result.php',
    data    : stuff,
    success : function(response) {
        alert(response);
    }    
});
您可以按照以下方式处理传递到
result.php
的数据:

$data    = $_POST["result"];
$data    = json_decode("$data", true);

//just echo an item in the array
echo "key1 : ".$data["key1"];
2。直接传递对象:

AJAX调用:

$.ajax({
    type    : 'POST',
    url     : 'result.php',
    data    : {result:JSON.stringify(stuff)},
    success : function(response) {
        alert(response);
    }    
});
$.ajax({
    type    : 'POST',
    url     : 'result.php',
    data    : stuff,
    success : function(response) {
        alert(response);
    }    
});
$\u POST
数组直接在
result.php
中处理数据,如下所示:

//just echo an item in the array
echo "key1 : ".$_POST["key1"];

这里我建议第二种方法。但是您应该尝试这两种方法:-)

我想与大家分享一个对我有用的完整示例,以避免为每个PHP函数创建每个JavaScript函数

//在HTML端,一个来自链接的简单JavaScript调用

 <a href="javascript:CargaZona('democonllamada', 'tituloprin', {'key1':'value1','key2':'value2'})" >test</a>
<div id='tituloprin' >php function response here!</div>
//在phpfunc.php页面上

<?php

$params = "{'key1':'value1','key2':'value2'}"; 
$fc = 'def';
if (isset($_POST['fc']))   {    $fc = $_POST['fc']; }
if (isset($_POST['params']))   {    $params = $_POST['params']; }

switch ($fc) {
    default:
        call_user_func($fc,$params);
}

function democonllamada($params) {
    $params = json_decode("$params", true);
    echo "ok llegaron".$params['key1'];
}

?>


您可以使用JSON.stringify()将该数组传递给php,然后根据需要在那里进行处理。要将其发布到php,该数组必须是
key=>value
类型,并且格式正确,因此看起来像<代码>{“key1”:“value1”,“key2”:“value2”}
谢谢我所做的一切,现在它显示的数组类似于{“key1”:“value1”,“key2”:“value2”}.现在又如何发送post请求?
data:stuff
如果ajax部分正确调用url,则正确发送。@Jon是的,它正确调用url,但参数和源未定义。无法工作,或接近它。从javascript传递的任何数据都不是这种格式,除非是“字符串”类型。只需创建一个变量var passString=array.join(“,”);然后在jQueryAjax数据中发送到那里。如果没有更多的代码,这仍然无法为php正确索引。最好在javascript部分修复问题并提交适当的数据。我添加了其他可能的解决方案。请看一看。第二部分正确。对于第一部分,当提交到PHP时,键值对将转换为数组
$\u POST['key']='value'
,因此除非从API读取JSON或将JSON发送到javascript,否则使用该库是没有好处的,但是JS通过AJAX请求发送给PHP的大部分内容都会自动转换为数组。感谢PHP,我得到了这个数组([0]=>ques_5[1]=>10[2]=>ques_4[3]=>11)。现在我想将此数组分为键和值。您确定
键和
值是数组的可选值吗?…然后使用此循环…
$i=0;$result=array();而($ihai.I)我可以同时发送ajax帖子和模型表单吗?举个例子