首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过AJAX传递几个PHP动态变量

通过AJAX传递几个PHP动态变量
EN

Stack Overflow用户
提问于 2018-06-05 06:32:02
回答 1查看 60关注 0票数 0

我真的被以下内容卡住了。我有一个动态生成的表,它显示了一种购物清单,其中包含一些从API获得的数据,比如产品名称和产品价格。

当我点击购物按钮时,我想要一个模式,显示总结购买的文本,并单击确认按钮,将数据保存在数据库中。

在这种情况下,我将动态变量存储在元素id和/或value中。但是,如果我现在就想要-as --存储两个以上的变量呢?

所以,这是我的代码:

  <table class="sortable" id="myTable">

              <thead >
                <tr>
                    <th onclick="sortTable(0)">Product</th>
                    <th onclick="sortTable(1)">Price</th>
                    <th onclick="sortTable(1)">Amount</th>
                    <th onclick="sortTable(3)"></th>
                </tr>
            </thead>
                <tbody>

                            <?php

                            foreach ($result as $doc) {

                              $price = $doc['2. price'];
                              $name = $doc['1. symbol'];
                              $rd_price = round($price, 2);
                              ?>

                              <tr>
                                  <td><?php echo $name; ?></td>
                                  <td class="product_price"><?php echo $rd_price; ?></td>
                                  <td><input type="text" id= "product_qty_" name="qty" class="product_qty" value="0"></td>
                                  <td class="amount_sub_total">0</td>
                                  <td><button class="sbtn" data-id="sbtn" data-name="<?php echo $name; ?>" data-price="<?php echo $rd_price; ?>" data-toggle = "modal" data-target = "#myModal" onclick="showDetails(this)">
                                     <i class="fas fa-shopping-cart"></i>
                                  </button></td>
                              </tr>

                        <?php } ?>

            </tbody>
        </table>
    </div>
</div>

<!-- Modal -->
<div class = "modal fade" id = "myModal" tabindex = "-1" role = "dialog"
   aria-labelledby = "myModalLabel" aria-hidden = "true" data-backdrop="false" style="">

   <div class = "modal-dialog">
      <div class = "modal-content">

         <div class = "modal-header">
           <h4 class = "modal-title" id = "myModalLabel">
              Purchase order
           </h4>


            <button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true" style="background-color: transparent; margin-top: -20px; float: right; color: #4d4646; width: 10px; height: 10px;">
                  &times;
            </button>


         </div>

         <div class = "modal-body">
            <p><span id="value"></span></p>
         </div>
         <div class = "modal-footer">
                     <button type = "button" id="modal_yes_btn" class = "btn btn-primary" style="width: 80px; height: 40px;" onclick="yesBtnFunction()">
                        Yes
                     </button>

                     <button type = "button" class = "btn btn-default" data-dismiss = "modal" id="modal_no_btn" style="width: 80px; height: 40px;" onclick="noBtnFunction()">
                        No
                     </button>

                  </div>

               </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->

</div><!-- /.modal -->

<script>

var product_quantity;
var sub_total;
$( document ).ready(function() {

    $(document).on("input paste keyup", ".product_qty", function( event ) {


        var product_price = 0;
        var gst_amount = 0;

        var sub_total = 0;
        var total_qty = 0;
        var grand_total = 0;


        product_quantity = $(this).val();
        product_price = $(this).parent().prev().html();

        sub_total = product_price * product_quantity;

        var sbt_display = $(this).parent().next().html(sub_total);
        $(this).parent().next().html(sub_total)

  });
});

function showDetails(button) {
var name = $(this).attr("data-name");
var price = $(this).attr("data-price");
var selectedAmount = product_quantity;

    $.ajax({
    url: "customer.php",
    method: "GET",
    data: {"name": name, "price": price, "selectedAmount": selectedAmount},
    success: function(response){
      $("#value").text(response);
    }
});

}

 </script>

customer.php

<?php
$name = $_GET["name"];
$price = $_GET["price"];
$selectedAmount = $_GET["selectedAmount"];
$totalprice = $price * $selectedAmount;

echo "You are about to purchase " . $selectedAmount . " shares of " . $name . " at $ " . $price . " per unit. " . "Total price: $ " . $totalprice .  " Do you want to proceed?";

 ?>

但是,如果我需要存储两个以上的值,或者我只想留下一个非变量Id,我该怎么做呢?

在此之前,非常感谢您。

EN

回答 1

Stack Overflow用户

发布于 2018-06-05 07:21:03

您可以像这样将它们作为attr添加到元素中

<button data-id="id" data-amount="amount" data-price="price"> send</button>

JS

function showDetails(button) {
var customerNumber = $(this).attr("data-id");
var customerPrice = $(this).attr("data-price");
var selectedAmount = $(this).attr("data-amount");

    $.ajax({
    url: "customer.php",
    method: "GET",
    data: {"customerNumber": customerNumber, "customerPrice": customerPrice, "selectedAmount": selectedAmount},
    success: function(response){
      $("#value").text(response);
    }
});

别忘了传递给函数$(this)而不是this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50689653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档