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条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题