duanli9930 2019-02-25 23:26
浏览 90
已采纳

将数据从jQuery传递到PHP文件中

I have a table and some <td> have a data, but not all of them. On a button click I run jQuery function which checks each <td> and where the data is present, grabs the data. After that the data is being passed to php file and inserted into my DB. Everything works great.

function insert_data() {
  if(confirm("
Are you sure?.
")) {
    $("#myTable td").each( function() {
      var worker = $(this).attr("id");
      var quarter = $(this).attr("title");
      var station = $(this).attr("name");
      var type = $(this).attr("class");
      $.post({ url: "insert_data.php", data: {worker:worker, quarter:quarter, station:station, type:type} });
    });
  }
  else { return false; }
}

I am wondering if instead of calling the php with ajax for every <td>, maybe there is a way to pass the data like one package? I checked at least couple dozen different articles here and on other websites and it seems that very often JSON is used for that purpose. I've never worked with JSON and after several days of trying different approaches, still can't figure out what I am doing wrong. I will appreciate any help. All I need is to pass data from my table into php file (and unpack it in there). I do not need to display it simultaneously on the html page.

Here is one of the versions which doesn't work:

JS:

function insert_data() {
  if(confirm("
Are you sure?.
")) {
    var myArray = []; // var to store all records for json data transfer
    $("#myTable td").each( function() {
      var worker = $(this).attr("id");
      var quarter = $(this).attr("title");
      var station = $(this).attr("name");
      var type = $(this).attr("class");
      var record = {worker:worker, quarter:quarter, station:station, type:type}; // sd - short for schedule data
      myArray.push(record); // add every record to same array
    });
      console.log(myArray);
      $.post({ url: "insert_data.php", data: {myArray: myArray }, success: function(data){ alert('Items added'); }, error: function(e){ console.log(e.message); } });
  }
  else { return false; }
}

In console I see following data (it looks like the data is being added to the array without issues):

(4) [{...}, {...}, {...}, {...}]
0: {worker: "556", quarter: "1", station: "abc_15", type: "rework"}
1: {worker: "147", quarter: "2", station: "abc_37", type: "rework"}
2: {worker: "345", quarter: "3", station: "abc_15", type: "rework"}
3: {worker: "12", quarter: "4", station: "abc_15", type: "rework"}

PHP:

  $mySchedule = array();
  $mySchedule[] = $_POST["myArray"];  // get the json array
  var_dump($mySchedule);
  foreach ($mySchedule as $sched) {
    $worker = $sched['worker']; // or: $sched->worker; - doesn't change the result
    $quarter = $sched['quarter'];
    $station = $sched['station'];
    $type = $sched['type'];
    // code to insert data into my DB - works fine when I pass data one by one instead of array
  }

HTML: I also added this script to the page with my table:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-json/2.6.0/jquery.json.min.js"></script>

I am not sure if it is needed or not.

-- It feels that the problem is in the way how I "unpack" the array. But I am not sure... I tried to follow all advises I could find here, but maybe I just miss something really important. I tried:

$mySchedule[] = json_decode($_POST["myArray"]); // in insert_data.php

data:  { json: JSON.stringify(myArray) } // in jQuery function

and some other advises...

</div>
  • 写回答

1条回答 默认 最新

  • doubo3384 2019-02-26 18:11
    关注

    update

    I got some help from one of my colleges. So, the jQuery code stayed without changes. PHP code had couple minor changes and it works fine now. The changes in PHP:

    $mySchedule = $_POST["myArray"];  // get the json array
    

    instead of:

    $mySchedule = array();
    $mySchedule[] = $_POST["myArray"];  // get the json array
    

    That is it. Thank you very much for help and advises. I hope this example will be helpful to others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据