Jquery AJAX 发布到 PHP [英] Jquery AJAX post to PHP

查看:33
本文介绍了Jquery AJAX 发布到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

OK i've got my json string built but i'm not sure what to do next??

$('#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代码:

In my php file i was attempting to write the dataString to a text file so i could see its coming through ok but nothing was in the text file!? Am i doing something wrong client side or PHP side, my php code:

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

推荐答案

为什么不尝试像这样构建数据

Why don't you try constructing your data like this

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 调用中

Then in your AJAX call

data: postData,

现在您的 PHP 脚本可以将数据作为多维数组进行处理

Now your PHP script can process the data as a multi-dimensional array

<?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
    }
}

这篇关于Jquery AJAX 发布到 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆