2017-08-09 131 views
1

所以基本上我正在做的是使用AJAX自動填充文本框以從調用C函數的PHP腳本中獲取信息。如何使用調用PHP腳本的AJAX接收數組?

這是我在理論上已經找到:(假設只接收到一個值)

$(document).ready(function(){ 
    window.setInterval(function(){ 
     var ajaxurl = 'php/portserverclient.php', 
     $.post(ajaxurl, NULL, function (response) { 
      $('#v1').val(response); 
     }); 
    }, 5000); 
}); 

現在,如果這個工程,我相信會的。如果我收到一個值的數組,那麼函數內部的輸入不能是響應,是正確的?那麼,我需要改變它以使其成爲一個數組?

爲了清楚起見,我的PHP腳本使用echo來輸出它的信息。我寧願以如V1 = 120,V2 = 120等更「標準」的方式輸出,但PHP對我來說是新的,我目前正在研究。謝謝。

編輯: 只是爲了更清楚

會是這樣的工作?

$(document).ready(function(){ 
    window.setInterval(function(){ 
     var ajaxurl = 'php/portserverclient.php', 
     $.post(ajaxurl, NULL, function (response[]) { 
      $('#v1').val(response[0]); 
      $('#v2').val(response[1]); 
      $('#v3').val(response[2]); 
     }); 
    }, 5000); 
}); 
+0

如果你正在使用'echo',響應是'string' ... –

+0

你可以請將我指向正確的方向,以便搜索將響應轉換爲數組? – Rayaarito

+0

當然...我完成編輯我的答案...嘗試一下;) –

回答

1

由於您在PHP端的echo,響應只是一個字符串。
但是,如果該字符串如果形成爲有效的JSON,則您可以像使用它那樣使用它。

所以在PHP端,確保JSON格式是有效的:

$array = [120,340,800]; 

echo json_encode($array); 

然後在JS ...你收到一個字符串...你必須分析它,使之數組。

$(document).ready(function(){ 
    window.setInterval(function(){ 
     var ajaxurl = 'php/portserverclient.php', 
     $.post(ajaxurl, NULL, function (response[]) { 
      var responseArray = JSON.parse(response); 
      $('#v1').val(responseArray[0]); 
      $('#v2').val(responseArray[1]); 
      $('#v3').val(responseArray[2]); 
     }); 
    }, 5000); 
}); 
0

根據OP更新,您可以嘗試類似的方式將數組中的每個項目映射到您可以執行的相應文本框。

$.post(ajaxurl, NULL, function (response) { 
     for (var i = 0; i < response.length; i++) { 
      $("#v" + (i + 1)).val(response[i]); 
     } 
    }); 

這會將從JSON端點返回的數組的每個索引映射到相應的文本框。

如果從端點返回的JSON是有效的JSON數組,則您的響應變量應該已經是一個數組!

+0

嘿!謝謝你的快速反應。我編輯了OP,使其更清晰一些,我有多個文本框。 V1,V2,V3等我想要提交的文件。我想問一個更明確的方式來問我想實現的是,我將'.post'函數內的'response'變成一個數組? – Rayaarito

+0

查看我的更新,並讓我知道你是否有任何問題 – Kulix

0

JavaScript和PHP之間進行通信的最簡單方法是JSON。 所以你的PHP腳本必須以這種格式生成答案。

PHP代碼

// At the top of your PHP script add this 
// that will tell to your browser to read the response as JSON 
header('Content-Type : application/json', true); 
// Do your logic to generate a PHP array 
echo json_encode($yourArray); 

HTML代碼

<div class="someClass"></div> 

Javascript代碼

var container = $('.someClass'); 
$.post(ajaxurl, NULL, function (response) { 
    console.log(response); // for debuging 
    for (let i = 0; i <= response.length; i++) { 
     let myItem = response[i]; 
     container.append('<p>' + item + '</p>'); 
    } 
}); 

這是乾淨的動態生成p元素,因爲你不知道有多少結果你的PHP文件將返回你。

我不知道的JavaScript代碼,你也許會收到,你必須轉換爲JavaScript數組

鏈接你的JavaScript到PHP腳本之前JSON字符串,嘗試一些呼叫與郵差(或其他人HTTP客戶端),以確保您的 'Web服務' 工作爲例外

0

發送您的數組作爲JSON:

echo json_encode(array($value1, $value2, $value3)); 

JS

$.post(ajaxurl, NULL, function (response) { 
    // selectors in same index order as response array 
    $('#v1, #v2, #v3').val(function(i){ 
     return response[i]; 
    });   
},'json'); 
0

您可以格式化在PHP中的字符串,使之返回一個JSON對象像

echo '{ "v1": "something", "v2": "some other thing", "v3": "blah blah" }';

前端:

$.ajax({ 
 
    url: 'yoururlhere', 
 
    type: 'POST', 
 
    data: NULL, 
 
    async: true, 
 
    dataType: 'json', 
 
    success: function(data) { 
 
     $('#v1').val(data.v0); 
 
     $('#v2').val(data.v1); 
 
     $('#v3').val(data.v2); 
 
    }, 
 
    error: function(data) { 
 
     alert('An error occurs!'); 
 
    } 
 
});

對不起,使用.post的$長版本,我覺得這樣我能更清楚