php - Jquery AJAX 发布到 PHP

标签 php jquery ajax json post

好的,我已经构建了我的 json 字符串,但我不确定下一步该做什么??

$('#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);
?>

最佳答案

应该这样做:

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

您可以尝试验证:

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

关于php - Jquery AJAX 发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4105211/

相关文章:

php - 在两个表之间移动整行

php - 使用同一个类的多个 Singleton mysql 连接

javascript - 结合砌体,imagesLoaded 与 ajax 功能

javascript - 我如何获得文本区域的高度

php - AJAX:单页应用程序结构/安全

javascript - PHP 将变量返回给 AJAX(也许是一个数组?)

javascript - 为网页实现类似 "server push"行为的最简单方法是什么?

php - 删除多个文件

php - 我应该如何存储密码盐?

jquery - 简单的 CouchDB + jQuery 移动应用程序可以在桌面浏览器中正常加载列表,只是不能在移动设备上加载