Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于重复值,Ajax第二次不起作用_Javascript_Php_Ajax - Fatal编程技术网

Javascript 对于重复值,Ajax第二次不起作用

Javascript 对于重复值,Ajax第二次不起作用,javascript,php,ajax,Javascript,Php,Ajax,当我通过ajax提交此表单时,它只得到第一项。我是新来的,所以从最后三天开始,我在网上寻找解决方案。请有人帮帮我 <?php include 'db.php'; $result = $mysqli -> query("SELECT * FROM AlmeenaShop LIMIT 8 "); if($result -> num_rows <1){ /*echo"<h2 >No Record F

当我通过ajax提交此表单时,它只得到第一项。我是新来的,所以从最后三天开始,我在网上寻找解决方案。请有人帮帮我

<?php
        include  'db.php';

        $result = $mysqli -> query("SELECT * FROM AlmeenaShop LIMIT 8 ");
        if($result -> num_rows <1){
        /*echo"<h2 >No Record Found</h2>";*/
        }else{




        ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Insert data in MySQL database using Ajax</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
    <div style="margin: auto;width: 60%;">
        <div class="alert alert-success alert-dismissible" id="success" style="display:none;">
          <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
        </div>
        <?php foreach ($result as $row ) { ?>
        <form id="fupForm" name="form1" method="post">

            <div class="single-products">
                            <div class="productinfo text-center"><img width="200" height="250" alt="" src="<?php  echo  $row['ImageLocation'] ; ?> " />
                                <?php if ($row['OfferPrice']>0): ?>
                                <h2><?php  echo "&#2547; ".  $row['OfferPrice']  ." /-"; ?><del class="text-primary"><?php  echo "&#2547; ".  $row['SalesPrice']  ." /-"; ?></del></h2>
                                <?php else : ?>
                                <h2><?php  echo "&#2547; ".  $row['SalesPrice']  ." /-"; ?></h2>
                                <?php endif ?>

                                <p><?php  echo  $row['ProductName'] ; ?></p>
                                <p><input type="number" name="ProductQuantity" value="1" id="ProductQuantity<?php  echo  $row['StoreID'] ; ?>"  step="1"></p>
                                <p><input type="hidden" id="StoreID<?php  echo  $row['StoreID'] ; ?>" name="StoreID" value="<?php  echo  $row['StoreID'] ; ?>"></p>

                            </div>
                        </div>
            <input type="button" name="target" class="btn btn-primary" value="Save to database" id="<?php  echo  $row['StoreID'] ; ?>">
        </form>
    </div>

            <?php
            } /* FOr Whole Loop*/
            }/* for if then else loop*/

            ?>

    <script>
    $(document).ready(function() {

        $('.btn').on('click', function() {
            var target = $(".btn").attr("id");
            $("#"+target).attr("disabled", "disabled");

            var StoreID = $('#StoreID'+target).val();
            var ProductQuantity = $('#ProductQuantity'+target).val();


            if(StoreID!="" && ProductQuantity!=""){
                $.ajax({
                    url: "MyCart.php",
                    type: "POST",
                    data: {
                        StoreID: StoreID,
                        ProductQuantity: ProductQuantity

                    },
                    cache: false,
                    success: function(){
            alert("ok");

                    }
                });
            }
            else{
                alert('Please fill all the field !StoreID: StoreID , ProductQuantity: ProductQuantity');

            }
        });
    });
    </script>




    </body>
    </html>

使用Ajax在MySQL数据库中插入数据
" />


$(文档).ready(函数(){
var buttons=document.getElementsByTagName(“按钮”);
var buttonsCount=buttons.length;
对于(var i=0;i
阿尔梅纳商店
阿尔梅纳商店
我们的产品


重复值的第二次
是什么意思?
?您能详细说明一下吗?您目前面临的问题是什么?先生,我需要用PHP中的submit按钮将一个shop页面发送到我的cart页面。但是如果我用PHP提交表单,它将100%用于发送和存储到DB。这是我的问题,PHP在提交后重新加载/刷新页面正在刷新表单。我需要避免这种刷新/重新加载。
$(“.btn”).attr(“id”)
将仅获取该类的第一个元素的值-这就是
attr
方法的工作方式。由于您希望获取刚刚单击此处的同一按钮的值,因此可以简单地使用
$(this.attr(“id”)
此处为本例。此处是否支持将其作为您问题的答案/解决方案?如果不是,请不要将其作为答案发布,而是编辑您的问题,如果您希望包含更多代码以保持清晰。
 <script>
$(document).ready(function() {
var buttons = document.getElementsByTagName("button");
var buttonsCount = buttons.length;
for (var i = 0; i <= buttonsCount; i += 1) {
buttons[i].onclick = function(e) {
//alert(this.id);
    var targetid= this.id;

    var PImg = $('#PImg_'+targetid).val();
    var PName = $('#PName_'+targetid).val();
    var PRPrice = $('#PRPrice_'+targetid).val();
    var POPrice = $('#POPrice_'+targetid).val();
    var PQ = $('#PQ_'+targetid).val();
    var PSID = $('#PSID_'+targetid).val();

  //alert(this.id+"StoreID"+StoreID+"ProductQuantity"+ProductQuantity);

          if(PSID!="" && PQ!=""){
            $.ajax({
              url: "MyCart.php",
              type: "POST",
              data: {
                PImg: PImg,
                PName: PName,
                PRPrice: PRPrice,
                POPrice: POPrice,
                PQ: PQ,
                PSID: PSID


              },
              cache: false,
              success: function(){
          //alert("ok");
            window.location.reload();
              }
            });
          }
          else{
          //  alert('Please fill all the field !');
          }

    }

};
});
</script>