2010-11-05 130 views
6

好吧,我已經有了我的json字符串,但我不知道下一步該怎麼做?Jquery AJAX發佈到PHP

$('#submit').live('click',function(){ 

       var dataString = '['; 
        $('#items tr').not(':first').each(function(){ 
         var index = $('#items tr').index(this); 
         var supp_short_code=$(this).closest('tr').find('.supp_short_code').text(); 
         var project_ref=$(this).closest('tr').find('.project_ref').text(); 
         var om_part_no=$(this).closest('tr').find('.om_part_no').text(); 
         var description=$(this).closest('tr').find('.description').text(); 
         var cost_of_items=$(this).closest('tr').find('.cost_of_items').text(); 
         var cost_total=$(this).closest('tr').find('.cost_total').text(); 
         dataString += '{"row":"' + index + '", "supp_short_code":"' + supp_short_code + '", "project_ref":"' + project_ref + '", "om_part_no":"' + om_part_no + '", "description":"' + description + '", "cost_of_items":"' + cost_of_items + '", "cost_total_td":"' + cost_total + '"}'; 
        }); 
        dataString += ']'; 

       $.ajax 
        ({ 
        type: "POST", 
        url: "order.php", 
        data: dataString, 
        cache: false, 
        success: function() 
         { 
          alert("Order Submitted"); 
         } 
        }); 
      }); 

在我的PHP文件我試圖寫dataString到一個文本文件,所以我可以看到它通過確定,但沒有到來是在文本文件中!?我做得不對客戶端或PHP身邊,我的PHP代碼:

<?php 
    $stringData = $_POST['dataString']; 
    $myFile = "testFile.txt"; 
    $fh = fopen($myFile, 'w') or die("can't open file"); 
    fwrite($fh, $stringData); 
    fclose($fh); 
?> 
+0

使用Firebug檢查,而不是寫入文件。 – Petah 2010-11-05 11:00:52

+0

是的,我可以在Firebug中看到它,但我只是想確認PHP是否正常 – benhowdle89 2010-11-05 11:02:31

回答

9

你爲什麼不嘗試構建這樣

var postData = {}; 
$('#items tr').not(':first').each(function(index, value) { 
    var keyPrefix = 'data[' + index + ']'; 
    postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text(); 
    postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text(); 
    // and so on 
}); 

然後在你的AJAX的數據呼叫

data: postData, 

現在你的PHP腳本能夠處理這些數據作爲多維陣列

<?php 
if (isset($_POST['data']) && is_array($_POST['data'])) { 
    foreach ($_POST['data'] as $row => $data) { 
     echo $data['supp_short_code']; 
     echo $data['project_ref']; 
     // and so on 
    } 
} 
9

這應做到:

... 
$.ajax({ 
    type: "POST", 
    url: "order.php", 
    data: { 'dataString': dataString }, 
    cache: false, 
    success: function() 
     { 
      alert("Order Submitted"); 
     } 
    }); 

您可以嘗試驗證:

<?php 
    $stringData = $_POST['dataString']; 
    echo $stringData; 
?> 
+1

很酷,我想我需要在PHP端使用json_decode嗎? IE瀏覽器。 '爲每個「行」做這種事情...... – benhowdle89 2010-11-05 11:12:46

+0

如果你想解析字符串中的數據,你應該使用json_decode然後 – jerjer 2010-11-05 11:56:52

1

的問題會是因爲你試圖訪問一個不存在的名爲「dataString」的POST變量。僅僅因爲你將「data」屬性設置爲名爲「dataString」的變量的內容並不意味着你的post變量將被稱爲「dataString」。

你可以試試這個:

data: { "dataString": dataString }, 

這傳遞一個對象,有一個叫「dataString」屬性和實際數據字符串的值的jQuery函數。 jQuery將從該對象中獲取所有屬性(在本例中僅爲一個),並將它們設置爲HTTP請求上的後置變量,最終將它們發送到您的PHP應用程序。這允許您通過$ _POST [「dataString」]調用訪問數據。

史蒂夫

4

首先,JSON對象轉換爲字符串,在JS是這樣的:

var json_string=JSON.stringify(json_object); 

然後,把它傳遞給PHP作爲一個字符串,然後在PHP解碼,就像這樣:

<?php 
    $map = json_decode($_POST['json_string']); 
?> 

希望這可以幫助任何人只是發現這個線程...

0

我有問題,當使用:

url: "/folder/form.php", 

,我必須使用:

url: "folder/form.php",,