doushi8187 2010-11-04 16:35
浏览 80
已采纳

构建json使用for循环发送到php(也许)

I've googled around, but i can find a way to build json with jQuery and send it to php using a for loop. I have a html table, and i want to take values from each row (i dont know how many rows/values there will be) construct a json string, ie. [{"row": 1, "supp_short_code" : blah blah....}, {"row": 2, ....} ....] but i dont know how to keep adding to the json datastring each time jQuery finds more values if that makes sense??

EDIT:

So if i have this

$('#submit').live('click',function(){ 
                    var supp_short_code=$('.supp_short_code').text();
                    var project_ref=$('.project_ref').text();
                    var om_part_no=$('.om_part_no').text();
                    var description=$('.description').text();
                    var cost_of_items=$('.cost_of_items').text();
                    var cost_total=$('.cost_total').text();
                    var dataString = 'string=//' + supp_short_code + '//' + project_ref + '//' + om_part_no + '//' + description + '//' + cost_of_items + '//' + cost_total

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

So what (roughly) would i need to change in this code? Screenshot

Ok, so as you can see by the screenshot, i'm using jquery to dynamically add the bottom table when a user click a row from the top table, its calculating totals and they can specify which supplier they want to use. I'm then using jquery to grab these values into the $('submit') bit of jquery code at the top. I then want to send these values to a php page that will somehow parse the received data at insert it into a mysql db, as something like "id 1 product blah price blah supplier blah total cost £x, id 2 product blah2 price blah2 supplier blah total cost £x" so some fields, ie the total cost and supplier will be the same, but the php might be receiving 3 different products for the same order if that makes sense? Thanks!

  • 写回答

2条回答 默认 最新

  • dongshi9719 2010-11-04 16:37
    关注

    You don't need to build the json, just build the data array and send it with .post, .get or .ajax. jQuery will take care of encoding the array for you:

    var data = {};
    
    for (var i = 0; i < 3; ++i) {
       data['field' + i] = 'value' + i;
    }
    
    $.post ('http://mysite.com/page', data, function() { alert('succes!'); });
    

    Your server-side code's $_POST array will containing:

    array( 'field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3'); 
    

    For your example, I would reconsider sending the data as a string and instead send the data as well-formated key/value pairs. Your server-side code can more easily decide what to do with it, and your JS won't require a rewrite if the server-side code needs the string to be built differently.

    $.ajax ({
      type: "POST",
      url:  "order.php",
      data: {
       supp_short_code: $('.supp_short_code').text(),
       project_ref:     $('.project_ref').text(),
       om_part_no:      $('.om_part_no').text(),
       description:     $('.description').text(),
       cost_of_items:   $('.cost_of_items').text(),
       cost_total:      $('.cost_total').text()
      }
      //...
    });
    

    Update

    You could reduce the amount of typing by throwing your field names into an array, and appending the class name of any fields you want to include in the data array. Then loop over each of your table rows and extract the values:

    var fields = [ 'supp_short_code', 'project_ref', 'om_part_no',
      'description', 'cost_of_items', 'cost_total'];
    
    var data = [];
    
    // loop over each row
    $('#my-table tr').each(function (i, tr) {
      // extract the fields from this row
      var row = {};
      for (var f = 0; f < fields.length; ++f) {
        row[fields[f]] = $(tr).find('.' + fields[f]).val();
      }
    
      // append row data to data array
      data.push(row);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集