wordpress使用ajax单个请求而不是一次点击多个请求

时间:2015-02-02 12:40:41

标签: php ajax wordpress

我有像这样的数据库表

product name

id product_name

1   A
2   B
3   C
4   D

product_buying_price

id product_name  product_buying_price
1   A                               10
2   B                           12
3   C                               15
4   D                               18

product_selling_price

id product_name product_selling_price
1   A                               12
2   B                           13
3   C                               19
4   D                               23

所以我有这样的下拉列表

<select name="product_name" id="product_name">
    <option value="">Products</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
</select>

我想在这样的表格中显示产品购买价格和产品销售价格的值

<table>
    <tr>
        <td>
            <select name="product_name" id="product_name">
                <option value="">Products</option>
                <option value="A">A</option>
                <option value="B">B</option>
                <option value="C">C</option>
                <option value="D">D</option>
            </select>
        </td>
        <td class="product_buying_price"></td>
        <td class="product_selling_price"></td>
    </tr>
</table>

像这样我有大约3-4个这样的字段,我想得到产品的这些值。

在js文件中我制作了像这样的ajax

$('body').on('change','#product_name', function() {
    var selected = $(this).val;

    //Get product buying price
    $.post( test.ajaxUrl, { 'selected' : selected,  'action' : 'get_product_buying_price' }, function(data){
        data = $.parseJSON(data);
        selected.find('td.product_buying_price').html(data);
    });

    //Get product selling price
    $.post( test.ajaxUrl, { 'selected' : selected,  'action' : 'get_product_selling_price' }, function(data){
        data = $.parseJSON(data);
        selected.find('td.product_selling_price').html(data);
    }); 
});

内部功能我有这样的功能

function get_product_buying_price() {
    global $wpdb;
    $selected_product = $_POST['selected'];
    $get_product_price = $wpdb->get_row("SELECT `product_buying_price` FROM `product_buying_price` WHERE `product_name` = '.$selected_product.' ");
    $product_buying_price = $get_product_price->product_buying_price;
    echo json_encode($product_buying_price);
    exit;
}


function get_product_selling_price() {
    global $wpdb;
    $selected_product = $_POST['selected'];
    $get_product_price = $wpdb->get_row("SELECT `product_selling_price` FROM `product_selling_price` WHERE `product_name` = '.$selected_product.' ");
    $product_selling_price = $get_product_price->product_selling_price;
    echo json_encode($product_selling_price);
    exit;
}

这里的工作正常。但是你不认为一次点击多个ajax请求会让它变慢吗?我有大约3-4个请求进行一次更改。那么有人可以告诉我一些更聪明的方法来实现这个目标吗?任何帮助和建议都会非常明显。感谢。

1 个答案:

答案 0 :(得分:0)

嗯,你可以将这两个功能放在一起:

function get_product_prices() {
    global $wpdb;
    $selected_product = $_POST['selected'];
    $get_product_price = $wpdb->get_row("SELECT `product_buying_price` FROM `product_buying_price` WHERE `product_name` = '.$selected_product.' ");
    $product_buying_price = $get_product_price->product_buying_price;
    $get_product_price = $wpdb->get_row("SELECT `product_selling_price` FROM `product_selling_price` WHERE `product_name` = '.$selected_product.' ");
    $product_selling_price = $get_product_price->product_selling_price;
    echo json_encode(array("buying" => $product_buying_price, "selling" => $product_selling_price);
    exit;
}

$('body').on('change','#product_name', function() {
    var selected = $(this).val;

    //Get product buying price/ selling prices
    $.post( test.ajaxUrl, { 'selected' : selected,  'action' : 'get_product_prices' }, function(data){
        data = $.parseJSON(data);
        selected.find('td.product_buying_price').html(data.buying);
        selected.find('td.product_selling_price').html(data.selling);
    });
});

您还可以在页面加载时加载所有数据,并在不进行AJAX调用的情况下更改这些值(knockout在这些情况下工作得非常好。)

相关问题