在成功函数内的表中添加计数器

时间:2016-03-24 14:13:50

标签: javascript php jquery ajax

我正在将项目添加到数据库中并将它们附加到类似于表格的帐单中,以便为客户打印:

这是我的PHP代码:

header('Content-Type: application/json');
               //client info
               $client_name = $_POST['client_name'];
               $res = array();

               $phone_number = $_POST['phone_number'];
               $address = $_POST['address'];
               $date_now = $_POST['date_now'];
               //type info
               $sale_type = $_POST['sale_type'];

               $sale_details = $_POST['sale_details'];
               $price = $_POST['price'];
               $installment = $_POST['installment'];
               $first_pay = $_POST['first_pay'];

               //Warranty and Quantity
               $quantity = $_POST['quantity'];
               $facture_number = $_POST['facture_number'];

            //If warranty == yes and installment == yes
            try
            {
                   if(($installment=="yes"))
                   {



                        //Adding a sale
                       $saleSql = "INSERT INTO sales
                       (client_name, sale_type, sale_detail, price, facture_number,
                       quantity, installment, first_pay, warranty, warranty_start, 
                       warranty_finish, date_now, time_now, admin)
                       VALUES
                       (:client_name, :sale_type, :sale_details, :price, :facture_number,
                       :quantity, :installment, :first_pay, :warranty, :warranty_start, 
                       :warranty_finish, :date_now, now(), :admin)";

                       $prepSale = $conn->prepare($saleSql);
                       $prepSale->bindValue(':client_name', $client_name);
                       $prepSale->bindValue(':sale_type', $sale_type);
                       $prepSale->bindValue(':sale_details', $sale_details);
                       $prepSale->bindValue(':price', $price);
                       $prepSale->bindValue(':facture_number', $facture_number);
                       $prepSale->bindValue(':quantity', $quantity);
                       $prepSale->bindValue(':installment', $installment);
                       $prepSale->bindValue(':first_pay', $first_pay);
                       $prepSale->bindValue(':warranty', "yes");
                       $prepSale->bindValue(':warranty_start', $date_now);
                       $prepSale->bindValue(':warranty_finish', "");
                       $prepSale->bindValue(':date_now', $date_now);
                       $prepSale->bindValue(':admin', $admin);

                       $execSale = $prepSale->execute();

                       $saleId = $conn->lastInsertId();

                       //Adding into client debts table
                       $debtSql = "INSERT INTO client_debts(
                       client_name, phone_number, address, 
                       sale_type, sale_details, total_debts, facture_number,
                       date_now, time_now)
                       VALUES
                       (:client_name, :phone_number, :address, :sale_type, :sale_details, :total_debts, :fn,
                       :date_now, now()
                       )";

                       $prepDebt = $conn->prepare($debtSql);
                       $prepDebt->bindValue(':client_name', $client_name);
                       $prepDebt->bindValue(':phone_number', $phone_number);
                       $prepDebt->bindValue(':address', $address);
                       $prepDebt->bindValue(':sale_type', $sale_type);
                       $prepDebt->bindValue(':sale_details', $sale_details);
                       $prepDebt->bindValue(':total_debts', $price);
                       $prepDebt->bindValue(':fn', $facture_number);
                       $prepDebt->bindValue(':date_now', $date_now);

                       $execDebt = $prepDebt->execute();

                       $lastid = $conn->lastInsertId();

                       //Insert Into client_details
                       $sqlDetail = "INSERT INTO client_details
                       (client_id, client_name, payment, facture_number, date_now, time_now)
                       VALUES
                       (:client_id, :client_name, :payment, :fn, :date_now, now())";
                       $prepDetail = $conn->prepare($sqlDetail);
                       $prepDetail->bindValue(':client_id', $lastid);
                       $prepDetail->bindValue(':client_name', $client_name);
                       $prepDetail->bindValue(':payment', $first_pay);
                       $prepDetail->bindValue(':fn', $facture_number);
                       $prepDetail->bindValue(':date_now', $date_now);
                       $execPrepDet = $prepDetail->execute();


                        //var_dump($execPurch);
                        //header("Location: purchases.php?type=".$sale_type."&details=".$sale_details."&quantity=".$quantity."&price=".$price);
                        $sql = "UPDATE purchases SET sell_quantity = sell_quantity + :sq
                        WHERE sale_type = :sale_type AND sale_details = :sale_details";
                        $stmt = $conn->prepare($sql);
                        $stmt->bindValue(':sq', $quantity);
                        $stmt->bindValue(':sale_type', $sale_type);
                        $stmt->bindValue(':sale_details', $sale_details);
                        $sexec = $stmt->execute();

                        $res = array('saleId'=>$saleId, 'lastid'=>$lastid, 'sale_type'=>$sale_type, 'sale_details'=>$sale_details,
                            'price'=>$price, 'date_now'=>$date_now, 'quantity'=>$quantity);
                        echo json_encode($res);

                   }

            }

我将结果附加到带有id=after_tr

的行后的表格中
$.ajax
            ({
                url:'insert.php',
                type:'post',
                data: {client_name: client_name1, phone_number:phone_number1, address:address1,
                    sale_type:sale_type1, sale_details:sale_details1, price:price1, first_pay:first_pay1,
                    facture_number:facture_number1, quantity:quantity1, installment:installment1,
                    date_now:date_now1},
                dataType: 'json',

                success:function(res)
                {
            //alert(res);
            $("#sale_type").val('');
            $("#sale_details").val('');
            $("#price").val('');
            $("#first_pay").val('');
            $("#quantity").val('');
            var count = 0;
            c = count + 1;
            $("#after_tr").after("<tr><td>"+c+"</td><td>"+res['sale_type']+"</td><td>"+res['sale_details']+
              "</td><td>"+res['price']+"</td><td>"+res['date_now']+"</td><td>"+res['quantity']+"</td></tr>");
                },
                error:function(res)
                {
                    console.log(res);
                }
            });

现在,我在每次插入后遇到行计数问题,我总是得到以下结果:

第1行=&gt; c=1 第2行=&gt; c=1

enter image description here

如何为这些附加行添加每个添加行的计数器?

0 个答案:

没有答案