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

Php wordpress使用ajax单个请求,而不是一次点击多个请求,php,ajax,wordpress,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

我有这样的数据库表

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>
$('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;
}
所以我有一个这样的下拉列表

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>
$('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;
}
内部函数我有这样的函数

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>
$('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个要求更改的请求。有人能告诉我一些更聪明的方法吗?任何帮助和建议都是非常值得的。谢谢。

您可以将这两个功能放在一起:

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调用