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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制