2010-08-16 99 views
0

我想從main.html發送數組到test.php文件並從test.php接收其他數組,但它不工作.plz幫助我!用jQuery發送和接收數組Ajax

main.html中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Untitled Page</title> 
    <script type="text/javascript" src="jquery/jquery-1.4.2.js"></script> 
    <script type="text/javascript"><!-- 
     $(function() { 
      $('#Button1').click(function() { 
       var SendArrary = []; 
       SendArrary[0] = $('#Text1').val(); 
       SendArrary[1] = $('#Text2').val(); 
       SendArrary[2] = $('#Text3').val(); 

       $.get('test.php', 
        SendArrary, 
         function (RecieveArray) { 
          $('#Text1').val(RecieveArray[0]); 
          $('#Text2').val(RecieveArray[1]); 
          $('#Text3').val(RecieveArray[2]); 
         }, 
         alert("Ajax Done!!!") 
        ) 

      }); 



     }); 

    --></script> 
</head> 
<body> 
    1<input id="Text1" type="text" /><br /> 
    2<input id="Text2" type="text" /><br /> 
    3<input id="Text3" type="text" /><br /> 
    <input id="Button1" type="button" value="button" /> 
</body> 
</html> 

test.php的

<?php 


$RecieveArray = $_GET['SendArrary']; 

$RecieveArray[0] = $RecieveArray[0]."1"; 
$RecieveArray[1] = $RecieveArray[1]."2"; 
$RecieveArray[2] = $RecieveArray[2]."3"; 


print_r($RecieveArray); 

?> 

回答

1

偷懶的辦法:

jQuery的:http://api.jquery.com/jQuery.get/

var callback = function(data){ 
    data = $.parseJSON(data); 
    // do stuff with data 
} 

$.get("test.php", { 
    array: SendArrary 
}, callback); 

PHP:http://www.php.net/manual/en/function.explode.php

$arr = explode(",", $_GET['array']); 

注意:你的反應必須是有效的JSON,如:

["one", "two", "three", 4] 

用PHP:http://php.net/manual/en/function.implode.php

echo("[".implode(",", $yourArray)."]"); 

你可能想看看PHP JSON工具包,如果你想冒險超越發送和接收陣列。

不知道如果php代碼能馬上工作,我只是在手冊中查看它,可能在這裏或那裏是錯誤的,但是如果你閱讀手冊鏈接,你應該得到開玩笑。