编辑PHP / AJAX以处理DELETE OR EDIT

时间:2015-04-29 09:08:50

标签: php ajax

首先,在我向您展示代码之前,我将解释我的网页是如何工作的。

  1. 用户选择日期 - > AJAX呼叫日期更改
  2. 结果PHP数据显示在页面的两个部分中。
  3. 第一部分是订单表内容
  4. 第二部分是项目表内容(不包括订单内的项目)
  5. 我想添加的是3个按钮的功能,它们将使用AJAX动态更改表格。 我目前正在处理非Ajax请求。

    以下是代码:

        $(document).ready(function(){
            $('.date-picker').change(function(){
                $.ajax({
                     type: 'POST',
                     url: 'php/getproduct.php',
                     data: {dateorderpicker: $('.date-picker').val()},
                     dataType: 'JSON',
                     success: function(data)
                     {
                        $("#cartrow").html(data.result_1);
                        $("#otheritems").html(data.result_2);
                     }
                 });
             });
         });
    

    当前AJAX的PHP文件:

    session_start();
        include('db_config.php');
        $datepicker = $_POST['dateorderpicker'];
    
        $sql = "SELECT * FROM orders WHERE deliveryDate = ? AND customerId = ? "; 
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1, $datepicker, PDO::PARAM_STR);
        $stmt->bindParam(2, $_SESSION['customer_id'], PDO::PARAM_INT);
        $stmt->execute();
    
        $container = array();
        $data['result_1'] = $data['result_2'] = '';
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            $container[] = "'{$row['itemName']}'"; // put them inside a temporary container
            $data['result_1'] .= '
                <div class="col-sm-4 col-md-4">
                    <div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
                        <div class="content-boxes-text">
                            <form action="php/edit.php" method="post" class="form-inline pull-right">    
                                <h3>' . $row['itemName'] . '</h3>
                                <h4>Total Price: $'.$row['price'].'</h4>    
                                <img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
                                <p>Our best seller.  Full of flavour.</p>
                                <div class="form-group">
                                <label class="sr-only" for="exampleInputAmount">Qty</label>
                                <div class="input-group">
                                <input type="number" name="qty" class="form-control" id="exampleInputAmount" value="' . $row['qty'] . '">
                                </div>
                                </div>
                                <input type="hidden" name="id" value="'.$row['id'].'">
                                <button type="submit" name="update" class="btn btn-primary">Update</button>
                                <button type="submit" name="delete" class="btn btn-primary">Remove</button>
                            </form>
                        </div>
                        <!-- //.content-boxes-text -->
                    </div>
                    <!-- //.content-boxes -->
                </div>
            ';
        }
    
        if(!empty($container)){
    
            $excluded_names = implode(',', $container);
            $sql = "SELECT * FROM item WHERE itemName NOT IN($excluded_names)";
            $stmt = $conn->prepare($sql);
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $price ="";
                if ($_SESSION['customer_band'] == 'A') {
                   $price = $row['bandA'];
                }
                else if ($_SESSION['customer_band'] == 'B') {
                    $price = $row['bandB'];
                } 
                else if ($_SESSION['customer_band'] == 'C') {
                    $price = $row['bandC'];
                }
                else if ($_SESSION['customer_band'] == 'D') {
                    $price = $row['bandD'];
                }
                else if ($_SESSION['customer_band'] == 'E') {
                    $price = $row['bandE'];
                }
    
                $data['result_2'] .= '
                <div class="col-sm-4 col-md-4">
                    <div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
                        <div class="content-boxes-text">
                            <form action="php/additem.php" method="post" class="form-inline pull-right">
                                <h4>'.$row['itemName'].'</h4><input id="itemname" type="hidden" name="itemName" value="'.$row['itemName'].'">
                                <h3>$'.$price.'</h3><input id="price" type="hidden" name="pricetotal" value="'.$price.'">
                                <img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
                                <p>'.$row['description'].'</p><input id="description" type="hidden" name="description" value="'.$row['description'].'">
                                <div class="form-group">
                                <label class="sr-only" for="exampleInputAmount">Qty</label>
                                <div class="input-group">
                                <input id="qty" type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
                                </div>
                                </div>
                                <button type="submit" id="additem" class="btn btn-primary">Add</button>
                            </form>
                        </div>
                        <!-- //.content-boxes-text -->
                    </div>
                    <!-- //.content-boxes -->
                </div>
            ';
            }
        }
        else 
        {
            $sql = "SELECT * FROM item";
            $stmt = $conn->prepare($sql);
            $stmt->execute();
    
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $price ="";
                if ($_SESSION['customer_band'] == 'A') {
                   $price = $row['bandA'];
                }
                else if ($_SESSION['customer_band'] == 'B') {
                    $price = $row['bandB'];
                } 
                else if ($_SESSION['customer_band'] == 'C') {
                    $price = $row['bandC'];
                }
                else if ($_SESSION['customer_band'] == 'D') {
                    $price = $row['bandD'];
                }
                else if ($_SESSION['customer_band'] == 'E') {
                    $price = $row['bandE'];
                }
    
                $data['result_2'] .= '
                    <div class="col-sm-4 col-md-4">
                        <div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
                            <div class="content-boxes-text">
                                <form action="php/additem.php" method="post" class="form-inline pull-right">
                                    <h4>'.$row['itemName'].'</h4><input type="hidden" name="itemName" value="'.$row['itemName'].'">
                                    <h3>$'.$price.'</h3><input type="hidden" name="pricetotal" value="'.$price.'">   
                                    <img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
                                    <p>'.$row['description'].'</p><input type="hidden" name="description" value="'.$row['description'].'">
                                    <div class="form-group">
                                    <label class="sr-only" for="exampleInputAmount">Qty</label>
                                    <div class="input-group">
                                    <input type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
                                    </div>
                                    </div>
                                    <button type="submit" id="additem" class="btn btn-primary">Add</button>
                                </form>
                            </div>
                            <!-- //.content-boxes-text -->
                        </div>
                        <!-- //.content-boxes -->
                    </div>
                ';
                }
        }
    
        echo json_encode($data);
        exit;
    

    更新和删除PHP文件:

    include('db_config.php');
    
    if (isset($_POST['update']))
    {
    $qty = $_POST['qty'];
    $id = $_POST['id'];
    
    echo $id;
    
    $sql = "UPDATE orders SET qty=? WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array($qty,$id));
    
    header('Location: ../order.php');
    }
    if (isset($_POST['delete']))
    {
    $id = $_POST['id'];
    
    $sql = "DELETE FROM orders WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array($id));
    
    header('Location: ../order.php');
    }
    

    上面的代码需要转换为AJAX,页面上使用ajax的两个部分都应该自动更新表。您可能会调用第一个ajax查询来正确重新加载表吗?

    感谢您查看此内容。 我无法理解如何开展这项工作。

    亚历

1 个答案:

答案 0 :(得分:1)

您可以轻松地为更新按钮提供课程(注意:是课程),类似于删除按钮

假设您的更新按钮具有“update_task”类

但是您的内容在DOM已经加载后添加到DOM中,因此您需要使用DELEGATE方法创建两个ajax请求以进行删除和更新。

供代表参考 - http://api.jquery.com/delegate/

// for update

 $("body").delegate(".update_task","click",function(){
   current_id = $(this).previous("input:hidden").val() // for current update button id,
       $.ajax({
             type: 'POST',
             url: 'php/update_product.php',
             data: {id: current_id, othervalues: other_value_of_choice},
             dataType: 'JSON',
             success: function(data)
             {
                if(data==1)
               {
                   // what ever you want to do if data has been updated
               }
             }
         });
 });
相关问题