I appreciate your question is quite old and you no doubt already have your solution but hopefully this may help someone else.
You could replace your javascript post
calls with a single call to a single function that extracted both parts of data and returned them as an object.
$('body').on('change','#product_name', function() {
//Get product prices
$.post( test.ajaxUrl, {
'selected' : $(this).val(),
'action' : 'get_product_prices'
}, function(data){
data = $.parseJSON(data);
$(this).parent('tr').find('td.product_buying_price').text(data.buying)
.end().find('td.product_selling_price').text(data.selling);
});
});
Then your php function would look like this.
function get_product_prices(){
global $wpdb;
$returnData = array();
$selected_product = esc_sql($_POST['selected']);
// please see the notes on your database structure
$get_product_price = $wpdb->get_row("SELECT `product_buying_price` FROM `product_buying_price` WHERE `product_name` = '.$selected_product.' ");
$returnData['buying'] = $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.' ");
$returnData['selling'] = $get_product_price->product_selling_price;
echo json_encode($returnData);
exit;
}
I’m not hugely familiar with the wpdb class; it may be possible to replace your sql queries with a single query like this
SELECT `product_buying_price`.`product_buying_price`, `product_selling_price`.`product_selling_price`
FROM `product_buying_price`
INNER JOIN `product_selling_price`
ON `product_buying_price`.`product_name` = `product_selling_price`.`product_name`
WHERE `product_buying_price`.`product_name` = '.$selected_product.'"
However from the look of your database tables, you appear to have an unnecessary number of tables and would be better to just store all data in one table
product_data
id product_name product_selling_price product_buying_price
1 A 12 10
2 B 13 12
3 C 19 15
4 D 23 18
I’d also apply some checks within the PHP and Javascript to ensure you have all the required data for input into your html table.