doupao5296 2018-10-12 10:01
浏览 46
已采纳

表中没有更新值,您与JS交互的次数越多

I have set up a table with html and populate the values with php.

The idea is to have the user able to update order statuses with a status and a message.

It works fine for first time, but the more you use it, it updates another row or not at all.

Not sure if it is an caching issue.

Please help!

Here is what I have:

                <?php $query = "SELECT user_orders.quote_id,user_orders.date_converted,user_orders.estimated_time_of_arrival,user_orders.order_balance,user_orders.status, user_orders.factory_message,
              users.user_id, users.user_firstname, users.user_lastname                 
             FROM user_orders
             INNER JOIN users ON user_orders.user_id=users.user_id WHERE user_orders.status != 0 ORDER BY date_converted DESC";

                $select_users = mysqli_query($connection, $query);
                while ($row = mysqli_fetch_assoc($select_users)) {
                    $quote_id = $row['quote_id'];
                    $date_converted = $row['date_converted'];
                    $estimated_time_of_arrival = $row['estimated_time_of_arrival'];
                    $user_id = $row['user_id'];
                    $user_firstname = $row['user_firstname'];
                    $user_lastname = $row['user_lastname'];
                    $total = $row['order_balance'];
                    $factory_message = $row['factory_message'];
                    $status = $row['status'];

                    ?>
                    <tr>


                        <td><?php echo $quote_id ?></td>
                        <td><?php echo $date_converted ?></td>
                        <td><?php echo $estimated_time_of_arrival ?></td>
                        <td><?php echo $user_firstname . " " . $user_lastname ?></td>
                        <td><?php echo $total ?></td>

                        <td><a href="/app/order_pdf.php?quote_id=<?php echo $quote_id ?>" target="_blank">View</a
                        </td>


                        <!--Current Status-->
                        <?php
                        if ($status === '2') {
                            echo "<td><span class=\"badge badge-success\">Approved</span></td>";
                        } elseif ($status === '3') {
                            echo "<td><span class=\"badge badge-danger\">Declined</span></td>";
                        } else {
                            echo "<td><span class=\"badge badge-warning\">Pending</span></td>";
                        }
                        ?>

                        <!--Change status-->
                        <td><select class="status" name="status">

                                <?
                                if ($status === "2") {

                                    ?>
                                    <option disabled>Please Select</option>
                                    <option value="1">Pending</option>
                                    <option value="2" selected>Accepted</option>
                                    <option value="3">Declined</option>

                                    <?

                                } elseif ($status === "3") {
                                    ?>

                                    <option disabled>Please Select</option>
                                    <option value="1">Pending</option>
                                    <option value="2">Accepted</option>
                                    <option value="3" selected>Declined</option>

                                    <?
                                } else {
                                    ?>
                                    <option disabled>Please Select</option>
                                    <option value="1" selected>Pending</option>
                                    <option value="2">Accepted</option>
                                    <option value="3">Declined</option>
                                    <?
                                }
                                ?>

                            </select></td>

                        <td><input class="message" name="message" style="width:100%;"
                                   value="<?php echo $factory_message ?>"></td>

                        <td style="text-align: left"><a href="" class="factoryUpdate">Update</a>
                        </td>

                    </tr>
                    <?php
                } ?>

                </tbody>

And the Js.

$(document).ready(function () {

    $(".factoryUpdate").click(function () {

        var quote_id = $(".quoteid", $(this).parent().parent()).val();
        var message = $(".message", $(this).parent().parent()).val();
        var status = $(".status", $(this).parent().parent()).val();

        var update = $(".update", $(this).parent().parent());

        $.ajax({

            url: "#",
            method: "POST",
            data: {

                "quoteid": quote_id,
                "message": message,
                "status": status
            },
            cache: false,
            success: function () {
                location.reload();
            }
        });


    });
});
  • 写回答

1条回答 默认 最新

  • duangao8359 2018-10-12 10:45
    关注

    You are using anchor tag as a button with an empty href, which essentially reloads the page. Sometimes, the reload might happen before the AJAX call gets made.

    Add an event object as argument to your onClick function and use preventDefault to prevent the browser from following the href link.

    $(document).ready(function () {
        $(".factoryUpdate").click(function (e) {
            e.preventDefault();
    
            var quote_id = $(".quoteid", $(this).parent().parent()).val();
            var message = $(".message", $(this).parent().parent()).val();
            var status = $(".status", $(this).parent().parent()).val();
    
            var update = $(".update", $(this).parent().parent());
    
            $.ajax({
    
                url: "#",
                method: "POST",
                data: {
    
                    "quoteid": quote_id,
                    "message": message,
                    "status": status
                },
                cache: false,
                success: function () {
                    location.reload();
                }
            });
        });
    });
    

    I hope this resolves your issue.

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

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导