Ajax响应时间太长

时间:2016-04-01 04:50:25

标签: php jquery ajax mysqli

    <table class="table table-bordered table-hover">

                            <tbody>
                                <tr>                        
        <td><select  name="ser" id="ser" class="bs-select form-control" onchange="getPrice(this.value);" data-live-search="true" required="">
        <option value="">--Select--</option>
        <?php
        $queryprd=$db->execute("select * from product_add where productcode LIKE '%$q%' and delet='0'");
        while($result=$queryprd->fetch_assoc())
        {
        ?>
        <option value="<?php echo $result['productcode']."|".$result['productname'];?>"><?php echo $result['productcode']."&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;".$result['productname'];?></option>
        <?php
        }
        ?>
        </select></td>
        <td><input type="text" name="sale" id="saleprice"  class="form-control" autocomplete="off" placeholder="Price"  required=""/></td>
        <td><select class="form-control msre" name="measure[]" id="measure">
            </select></td>
                                </tr>
                            </tbody>
    </table>
<script>
        function getPrice(val)
{
   $.ajax({
     type: 'post',
     url: 'get_sales_price.php',
     data: {
       get_option:val
     },
     success: function (response) {
       document.getElementById("saleprice").value=response; 
       getquty(val);
       getmsur(val);
     }
   });
}

function getmsur(val)
{
   $.ajax({
     type: 'post',
     url: 'get_measure.php',
     data: {
       get_option:val
     },
     success: function (response) {
        document.getElementById("measure").innerHTML=response;
        }
   });
}


</script>

get_sales_price.php

<?php
session_start();
ob_start();
require "../model/configuration.php";
$option = $_POST['get_option'];
list($code,$pname) = explode("|", $option);
$name=$pname;
$cod=$code;
$_SESSION['prdcode']=$cod;
$querydep=$db->execute("select * from productsale_price where product_code='$cod'");
$pprce=$querydep->fetch_assoc();
$queryty=$db->execute("select * from product_add where productcode='$cod' and delet='0'");
$resultro=$queryty->fetch_assoc();
$_SESSION['prdtype']=$resultro['product_type'];
$_SESSION['mrp']=$pprce['mrp'];
echo $pprce['sale_price'];
?>

get_measure.php

<?php
session_start();
ob_start();
require "../model/configuration.php";
$option = $_POST['get_option'];
list($code,$pname) = explode("|", $option);
$name=$pname;
$cod=$code;
       $prdqty=$db->execute("select * from master_purchase where product_code='".$cod."' and delet='0' order by master_purchase_id DESC");//and vendor_id='".$_SESSION['vnid']."'
       $tqty=$prdqty->fetch_assoc();
?>

<option value="">--Select--</option><option value="Box" <?php if($tqty['measure']=='Box'){ echo "selected";}?>>Box</option><option value="Ml"<?php if($tqty['measure']=='M'){ echo "selected";}?>>Ml</option>

此代码工作正常,但我的问题是....当我从选择框中选择一个选项时 id =“ser”我在 onchange =“getPrice中调用一个函数( this.value);“其工作完美ajax结果显示在指定字段中有时1或3分钟有时超过5分钟如何减少浪费时间
请帮忙......

2 个答案:

答案 0 :(得分:1)

使用LEFT join使用一个SQL,而不是使用两个SQL来获取数据,这样可以节省一些时间。

<?php
    $querydep=$db->execute("select pp.mrp, pp.sale_price, pa.product_type
    from productsale_price As pp LEFT JOIN product_add As pa 
       ON pa.product_code = pp.product_code AND pa.delet='0' 
        where pp.product_code='$cod'");
    $pprce=$querydep->fetch_assoc();
    $_SESSION['prdtype']=$pprce['product_type'];
    $_SESSION['mrp']=$pprce['mrp'];
    echo $pprce['sale_price'];
?>

答案 1 :(得分:0)

您是否为表productsale_price,product_add和master_purchase创建了索引?

如果没有,您需要索引:   - product_code上的productsale_price   - product_add on productcode and   - master_purchase product_code

这应该可以做到!当然,我假设您的SQL服务器在最佳条件下运行,并且不是导致响应缓慢的原因!