Ajax / PHP:如何在Ajax和PHP之间传递和接收数组(代码部分工作)

时间:2022-06-02 02:31:25

I am new to Ajax and PHP and hope someone can help me with this.

我是Ajax和PHP新手,希望有人能帮我。

I need to generate an array with unique IDs in jQuery, pass it to a PHP page and return the corresponding values from the PHP page.

我需要用jQuery生成一个具有唯一id的数组,并将其传递给PHP页面,并从PHP页面返回相应的值。

So far I have the following code which works on the jQuery side so the first console.log logs the input array correctly, e.g. ["1", "2", "3", "4", "5"].

到目前为止,我有以下代码,它在jQuery端工作,因此是第一个控制台。log正确地记录输入数组,例如["1"、"2"、"3"、"4"、"5"]。

However, I am not getting any results from the PHP page and the second console.log logs nothing for the output array so I guess I am missing one or more things there. The result should be an associative array where each item is listed with its tID and the corresponding value from the MySQL query, like "tID" => "1", "value" => "value1" ... .

但是,我没有从PHP页面和第二个控制台得到任何结果。log不记录输出数组的任何内容,所以我猜我漏掉了一个或多个东西。结果应该是一个关联数组,其中每个条目都与它的tID以及MySQL查询的相应值一起列出,如“tID”=>“1”,“value”=>“value1”…。

Can someone help me with this ?

有人能帮我一下吗?

JS side:

JS:

function getValues(languageFrm){
    var values = [],
        tID;

    $('.valuesDynamic').find('.values').each(function(){
        tID = $(this).attr('name').replace('tID', '');
        if($.inArray(tID, values) === -1){
            values.push(tID);
        }
    });
    values.sort();
    console.log(values);

    $.ajax({        
        type: "post",   
        url: "ajax.php",
        cache: "false",
        data: {
            node: 'fetchValues',
            values: values,
            languageFrm: languageFrm
        },
        success: function(data){
            console.log(data);
        }
    });
}

PHP side:

PHP的一面:

case "fetchValues":
    $values = $_POST["values"];
    $languageFrm = $_POST["languageFrm"];

    $stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN(?) ORDER BY tID");
    $stmt->bind_param("s", implode(",", $values));
    $stmt->execute();
    $result = $stmt->get_result();
    while($arrValues = $result->fetch_assoc()){
        $result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
    }

    echo $result;
    break;

Many thanks for any help.

非常感谢你的帮助。

3 个解决方案

#1


2  

You could JSON encode you array and then JSON decode on PHP side.

你可以JSON编码你的数组,然后JSON解码在PHP端。

For your JS side, you would replace your data section with:

对于你的JS方面,你可以将你的数据部分替换为:

 data: {
            node: 'fetchValues',
            values: JSON.stringify(values),
            languageFrm: languageFrm
        },

for PHP. instead of $values = $_POST["values"], you would do:

PHP。与$values = $_POST["values"]不同,您应该:

$values = json_decode($_POST["values"]);

#2


1  

I believe your PHP SIDE should be

我认为你的PHP应该是

case "fetchValues":
$values = implode(",", $_POST["values"]);
$languageFrm = $_POST["languageFrm"];

$stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN($values) ORDER BY tID");
$stmt->execute();
$result = $stmt->get_result();
while($arrValues = $result->fetch_assoc()){
    $result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
}

echo $result;
break;

In other words you must put $values directly in your query

换句话说,您必须将$值直接放入查询中

#3


0  

JS Side

JS的一面

data: {
            node: 'fetchValues',
            values: $.extend({},values),
            languageFrm: languageFrm
        },

PHP Side

PHP的一面

$result is an array. so replace echo $result; with print_r($result);

美元,结果是一个数组。所以美元取代回声的结果;print_r(结果);

#1


2  

You could JSON encode you array and then JSON decode on PHP side.

你可以JSON编码你的数组,然后JSON解码在PHP端。

For your JS side, you would replace your data section with:

对于你的JS方面,你可以将你的数据部分替换为:

 data: {
            node: 'fetchValues',
            values: JSON.stringify(values),
            languageFrm: languageFrm
        },

for PHP. instead of $values = $_POST["values"], you would do:

PHP。与$values = $_POST["values"]不同,您应该:

$values = json_decode($_POST["values"]);

#2


1  

I believe your PHP SIDE should be

我认为你的PHP应该是

case "fetchValues":
$values = implode(",", $_POST["values"]);
$languageFrm = $_POST["languageFrm"];

$stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN($values) ORDER BY tID");
$stmt->execute();
$result = $stmt->get_result();
while($arrValues = $result->fetch_assoc()){
    $result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
}

echo $result;
break;

In other words you must put $values directly in your query

换句话说,您必须将$值直接放入查询中

#3


0  

JS Side

JS的一面

data: {
            node: 'fetchValues',
            values: $.extend({},values),
            languageFrm: languageFrm
        },

PHP Side

PHP的一面

$result is an array. so replace echo $result; with print_r($result);

美元,结果是一个数组。所以美元取代回声的结果;print_r(结果);