2010-11-05 127 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",,