javascriptjqueryajaxjquery-uijquery-ajaxq

jquery is running multiple times


I'm sending a jquery ajax request and getting some js code as a response. I placed the response in a div $("div").html(data) and executed the script using $("div").find("script").each(function(){ eval($(this).text()); }); but i dont't know why the script is running twice. Here's my code that sends ajax request ad deal with the response

$.post("ajax-req-handler.php",
                {
                    key: "barcode-input-for-sale-return",
                    barcodeVal: $('.product-searchbar').val(),
                    soldQty: 1,
                    soldDisc: pickDisc,
                    soldPrice: pickPrice,
                    custNum: contNum,
                    saleDate: $(".sale-date").val()
                },
                function( data ){
                    $(".exec-script").html(data);
                    $(".exec-script").find("script").each(function(){
                        eval($(this).text());
                    });
                });

And here my ajax-req-handler.php file

            <script> 
                function deleteIfMatches(array, match){
                    array.some((ele)=>{ ele.indexOf(match) >= 0 ? array.splice(array.indexOf(ele),1): null });
                    return array;
                }
                var barIdFr = '<?php echo $barcode_id; ?>';
                var qtyToRetFr = <?php echo $quantity; ?>;
                var prodStrFr = '<?php echo $prod; ?>';
                var discountFr = '<?php echo $disc; ?>';
                var priceFr = '<?php echo $price; ?>';
                var cNumFr = '<?php echo $c_num; ?>';
                var dateFr = '<?php echo $sale_date; ?>';
                var prevRetAmtFr = $(".cust-info .atbr input").val().replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '');
                $(".cust-info .atbr input").val(parseInt(priceFr)+parseInt(prevRetAmtFr)+" /-");
                $("#total").val(parseInt(priceFr)+parseInt(prevRetAmtFr)+" /-");
                $("#paid").val(parseInt(priceFr)+parseInt(prevRetAmtFr)+" /-");
                $(".checkout-product-list").find("#"+barIdFr).find(".sold-qty").text("0");
                var prodArrFr = prodStrFr.split("-");
                var singleProdFr = prodArrFr.filter(function(item, index) {
                            return item.indexOf(barIdFr) == 0; 
                        });
                console.log(priceFr+"  "+prevRetAmtFr);
                var singleProdQtyFr = singleProdFr[0].split("/");
                var prevQtyFr = singleProdQtyFr[4];
                var currQtyFr = 1;
                var currPlusPrevFr = parseInt(currQtyFr) + parseInt(prevQtyFr);
                deleteIfMatches(prodArrFr, barIdFr);
                prodArrFr.push(barIdFr+"/"+0+"/"+discountFr+"/"+priceFr+"/"+currPlusPrevFr+"/"+0);
                $.post("ajax-req-handler.php",
                {
                    key: "updating-db-for-Sales-return-single-product",
                    arrFr: prodArrFr,
                    custIdFr: cNumFr,
                    dateFr: dateFr
                },
                function( dataFr ){
                //  alert(dataFr);
                });
            </script> 

Solution

  • You do not need to do

    $("div").find("script").each(function(){ eval($(this).text()); });

    <script> tags are executed when the HTML is parsed. I would recommend instead having logic in the script tag that finds the place where you are trying to insert HTML and then do the inserting.