douding7189 2016-09-21 10:03
浏览 69
已采纳

创建和循环json对象的问题

I am trying to create a json object in js, use ajax to send it to a php page and loop through it in the php page.

My code is as follows

form page

<html>
    <body>
    Input 1: <input type="text" data-id="1" data-old="30"/><br>
    Input 2:<input type="text" data-id="2" data-old="40"/><br>
    Input 3:<input type="text" data-id="3" data-old="50"/><br>

    <input type="text" id="max" value="3" hidden/>

    <button type="button" id="proceed">Go</button>
    </body>
</html>

js

$("#proceed").on("click",function(){
var num=$("#max").val();
var array=[];  //[] or {} ??
for(i=1;i<=num;i++){
    var old=$('input[data-id="'+i+'"]').data("old");
    var new=$('input[data-id="'+i+'"]').val();
    var deduct;
    if(new!==old && new!==""){
        deduct=old-new;
        array.push({"pid":i,"deduct":deduct});
    }

    var updateArray = JSON.stringify(array);
    $.ajax({
        type:"POST",
        url:"../control/stock-c.php",
        data:{updateArray:updateArray, func:"manualSync"},
        dataType:"json",
        success:function(){
            alert("yay");
        }        
    });

}
});

stock-c.php

require_once '../model/stock-m.php';
$updateArray=$_POST["updateArray"];
$array=  json_decode($updateArray);
$obj=new Stock();
foreach($array as $obj1){
    $result=$obj->deductMainstock($obj1->pid,$obj1->deduct);
}
return $result;

The Stock class exists in the the stock-m.php and I can confirm that the method deductMainstock() works (includes a mysqli update query).

However when running my code it seems that the deductMainStock() hasn't worked. I know there's a bulk of code so to keep it simple I need to know the following:

  1. Is the var array created in js file done properly?? I need to create a json object to send the details using ajax in the following format:

array=[{"pid"=1,"deduct"=a_value},{"pid"=2,"deduct"=another_value},{"pid"=3,"deduct"=another_value}]

  1. Is the ajax call in js file correct? I used JSON.stringify() to convert the above array into json for sending over ajax.

  2. Am I looping through the array properly in stock-c.php? I need to send "pid" and "deduct" as number variables to the deductMainstock() method.

  • 写回答

1条回答 默认 最新

  • dszdiavv474681 2016-09-22 03:51
    关注
    1. Please change the variable name var new to something else since its reserved keyword.
    2. var updateArray = JSON.stringify(array); You no need to stringify the array.
    3. Move the ajax call out side the for loop.

    Below is the updated code

    Script

    <script>
    $("#proceed").on("click",function(){
      var num=$("#max").val();
      var array=[];  //[] or {} ??
      for(i=1;i<=num;i++){
          var old=$('input[data-id="'+i+'"]').data("old");
          var new_val=$('input[data-id="'+i+'"]').val();
          var deduct;
          if(new_val!==old && new_val!==""){
              deduct=old-new_val;
              array.push({"pid":i,"deduct":deduct});
          }
    
      }
      if(array.length){
         $.ajax({
           type:"POST",
           url:"../control/stock-c.php",
           data:{updateArray:array, func:"manualSync"},
           dataType:"json",
           success:function(){
             alert("yay");
           }        
         });
      }
    
    });
    </script>
    

    PHP

    Post value will be array so remove the json_decode from your code.

    <?php 
    require_once '../model/stock-m.php';
    $array = $_POST["updateArray"];
    $obj = new Stock();
    foreach ($array as $obj1) {
        $result = $obj->deductMainstock($obj1->pid,$obj1->deduct);
    }
    return $result;
    
    ?>
    

    Hope this will help.

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

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角