[英]How do you pass an array to PHP from JQUERY and then send an array back to JQUERY from PHP
我無法想象我的生活。
我試圖將一個數組傳遞給php腳本processData.php,然后將使用該數據執行perl腳本。
然后我想將perl腳本的結果返回給JQUERY進行顯示。
看起來當我嘗試重新顯示數據時,數據會以NULL值的形式返回。
JQUERY PAGE:
$('#numDevices').click(function(event) {
event.preventDefault();
var test = [];
var $number = $('#element');
var value = $number.val();
var container = $('#main');
$('.device').remove();
$('#send').remove();
for(var i=0; i<value; i++) {
container.append('<div class="device"><fieldset><legend>Device' + i + '</legend><textarea class="input" rows="10">Please enter a hostname, followed by the corresponding links.</textarea></fieldset></div>');
}
container.append('<input type="submit" id="send" name="send" value="submit"/>');
$('#send').click(function(event) {
event.preventDefault();
var device = new Array();
$('textarea').each(function(index, area) {
var $area = $(area).val();
device.push($area);
});
$.ajax({
type: "GET",
url: "processData.php",
data: "input"+device,
cache: false,
success: function(data) {
alert(data);
},
error: function() {
alert("FAILED");
}
});
});
});
.....
PHP PAGE:
<?php
$data =$_GET['input'];
echo json_encode($data);
?>
所以你可以看到,為了測試,我試圖將一個數組傳遞給php,然后將它傳遞給JQUERY進行顯示。 當我傳回時,我得到一個NULL值,所以我只能假設我發送一個NULL值。
如果需要更多信息,我很樂意添加它。
UPDATE
1)我更改了data: "input"+device,
data: {input: device},
我現在可以通過var_dump($ data)看到值;
我現在遇到的問題是發送回JQUERY的數據是NULL。
第二次更新
1)添加: dataType: "json"
2)將GET
更改為POST
新錯誤:1)Parsererror 2)意外結束輸入3)[對象] [對象]
var device = $.map($('textarea'), function(el, i) { return el.value; });
$.ajax({
type: "GET",
url: "processData.php",
dataType: 'JSON',
data: {input : device.join('$$')},
cache: false
}).done(function(json) {
console.log(json);
});
PHP
<?php
$data = $_GET['input'];
echo json_encode( explode('$$', $data) );
?>
測試一下
$('#numDevices').click(function(event) {
event.preventDefault();
var test = [];
var $number = $('#element');
var value = $number.val();
var container = $('#main');
$('.device').remove();
$('#send').remove();
for(var i=0; i<value; i++) {
container.append('<div class="device"><fieldset><legend>Device' + i + '</legend><textarea class="input" rows="10">Please enter a hostname, followed by the corresponding links.</textarea></fieldset></div>');
}
container.append('<input type="submit" id="send" name="send" value="submit"/>');
$('#send').click(function(event) {
event.preventDefault();
var device = new Array();
$('textarea').each(function(index, area) {
var $area = $(area).val();
device.push($area);
});
$.ajax({
type: "POST",
url: "processData.php",
data: {input : device},
cache: false,
dataType: 'json',
success: function(data) {
alert(data[0]);
alert(data[1]);
},
error: function() {
alert("FAILED");
}
});
});
});
在PHP中
<?php
$data =$_POST['input'];
echo json_encode($data);
?>
好的我弄清楚出了什么問題......
PHP版本5.4未正確安裝
當我做了一個php -v它會告訴我5.4.7,但是如果我做了rpm -qa | grep php它會告訴我5.1.6版本。
我必須刪除rpm的5.6.1並安裝rpm的5.3並且它工作。
謝謝大家幫我解決這個問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.