json_decode之后的未定义索引

时间:2017-08-17 02:48:06

标签: javascript php jquery arrays ajax

当我试图获取JavaScript数组的内容并将其值传递给PHP数组变量时,我遇到了问题。

我正在做的是,当用户点击按钮时,点击的项目的值将被添加到javascript数组中,每次javascript数组更新时,PHP变量也会更新,因此显示PHP数组内容的div的内容。我没有使用表格而是按钮

但是,当我尝试使用json_decode函数获取JavaScript数组的值时,它会给我Message: undefined index: data错误

我是JavaScript,jQuery和AJAX的初学者,所以请耐心等待我

这是我的代码:

<script>
    // ARRAY VARIABLE WHERE ITEM VALUES WILL BE STORED
    var food_item = new Array();

    // FUNCTION TO BE TRIGGERED WHEN USER CLICKED THE BUTTON, GET THE ITEM'S VALUE AND ADD IT TO ARRAY
    function addToTray(data){
        food_item.push(document.getElementById(data).value);
        dataString = food_item; // array?
        var jsonString = JSON.stringify(dataString);
           $.ajax({
                type: "POST",
                url: "main",
                data: {data: jsonString}, 
                cache: false,

                success: function(){
                    alert("OK");
                }
            });
    }
</script>

<body>
    ...

    // LINE OF CODE RESPONSIBLE FOR FETCHING JAVASCRIPT ARRAY VALUES
    $this->session->userdata['food_tray'] = json_decode($_POST['data']);

    // DIV TO BE UPDATED

    <div class="col-md-3 food-tray" id="food-items" style="margin-top: 7%">
        <!--FOOD TRAY-->
        <div class="panel-heading">
            <h3 class="ft-header">Food Tray</h3>
        </div>
        <div class="panel-body">
            <?php
                $count_fi = count($this->session->userdata('food_tray'));

                echo "<p>Your Items (" . $count_fi . ")</p>";

                if($count_fi == 0){
                    echo "<p class='no-avail-list'>NO ITEMS ADDED IN FOOD TRAY</p>";
                }else{
                    echo "<table class='table borderless'>";

                    for($i = 0; $i < $count_fi; $i++){
                        echo "<tr>";
                        echo "<td>" . $this->session->userdata['food_tray'][$i] . "</td>";
                        echo "<td>P 200.00</td>";
                        echo "<td><form method='post'><input type='number' name='qty' min='1' max='100' value='1'/></form></td>";
                        echo "</tr>";
                    }

                    echo "</table>";
                    echo "<a href='' class='btn btn-success' data-toggle='modal' data-target='#myModal'>
                        Add Friend</a>";
                    echo "<a href='".base_url()."index.php/CheckOut' class='btn btn-warning'>Proceed to Checkout</a>";
                }
            ?>
        </div>
    </div>

    ...

    <?php
        foreach($query->result() as $row){
            echo "<div class='col-lg-7'>";
                echo "<div class='thumbnail'>";
                    $file_name = strtolower(preg_replace('/[^A-Za-z0-9\-.]/', '', $row->name));
                    /*if(file_exists(FCPATH . '/assets/images/main/food/' . $file_name . '.jpg')){
                        echo "<img src='".base_url()."/assets/images/main/food/".$file_name.".jpg' alt='' width='320px' height='100px'>";
                      }else{
                        echo "<img src='http://placehold.it/320x150' alt=''>";
                      }*/
                      echo "<img src='http://placehold.it/320x150' alt=''>";
                      echo "<div class='caption'>";
                        echo "<h5 class='pull-right'>&#x20B1 " . $row->price . "</h5>";
                        echo "<h5><a href='#'>" . $row->name . "</a></h5>";
                        echo "<h5>Calorie Count :  " . ($row->calorie_count == NULL ? "Not Available" : $row->calorie_count) . "</h5>";
                        // BUTTON FOR `addToTray()` FUNCTION
                        echo "<button class='btn btn-primary' id='".$row->id."' value='".$row->name."' style='width:100%' onclick='addToTray(".$row->id.")'>Add to Tray</button>";
                        // var_dump($row->name);
                      echo "</div>";
               echo "</div>";
           echo "</div>";
       }
    ?>
</body>

任何知道如何解决这个问题的人?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

创建会话
    thread  sequence      start      end
14       1       114    1647143  1672244
15       1       115    1672244  1689707
16       1       116    1689707  1713090
17       1       118    1735352  1760283
18       1       119    1760283  1788062
19       1       120    1788062  1789885
20       1       121    1789885  1790728

检查您的javascript库是否已在源选项卡中加载。