我正在尝试创建一个计算依赖于国家/地区的定价的页面。我有两个定价层,并希望在选择国家/地区时更新两个单独输入字段中的定价。我可以通过重复两次JQuery和business_plan_calculation函数来做到这一点,但是想知道是否有办法用一个函数做到这一点?
以下是我用两个函数
执行的代码function country_selector() {
global $wpdb;
$results = $wpdb->get_results("SELECT country FROM master_country ORDER BY country ASC");
foreach ($results as $rows) :
$option .= '<option value="'.$rows->country.'">';
$option .= $rows->country;
$option .= '</option>';
endforeach;
?>
<script type="text/javascript">
jQuery( document ).ready( function() {
jQuery( '#country_selector' ).change( function() {
var countryPOP = jQuery( '#country_selector' ).val();
jQuery.ajax( {
url: "<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php",
type: 'POST',
data: 'action=business_plan_calculation&countryID=' + countryPOP,
success: function( results ) {
jQuery( '#business_plus_price' ).empty();
jQuery( '#business_plus_price' ).val( results );
}
});
jQuery.ajax( {
url: "<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php",
type: 'POST',
data: 'action=enterprise_calculation&countryID=' + countryPOP,
success: function( results ) {
jQuery( '#enterprise_price' ).empty();
jQuery( '#enterprise_price' ).val( results );
}
});
});
});
</script> <?php
return '<select id="country_selector"><option value="0" disabled selected>Select Your Country</option>'.$option.'</select>';
}
add_shortcode('country-selector', 'country_selector');
function business_plan_calculation() {
if(isset($_POST['countryID'])) :
$parentCat=$_POST['countryID'];
global $wpdb;
$results = $wpdb->get_row( "SELECT currency_alpha, ppp_conversion FROM master_country WHERE country='$parentCat'" );
$endpoint = 'convert';
$access_key = '3a8c6c408b87705bde661d3d17b938ed';
$user_currency = $results->currency_alpha;
$chGBPUSD = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&from=GBP&to=USD&amount=1');
$chUSERGBP = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&from='.$user_currency.'&to=GBP&amount=1');
curl_setopt($chGBPUSD, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chUSERGBP, CURLOPT_RETURNTRANSFER, true);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $chGBPUSD);
curl_multi_add_handle($mh, $chUSERGBP);
$active = null;
do {
curl_multi_exec($mh, $active);
} while ($active);
$GBPUSD = curl_multi_getcontent($chGBPUSD);
$USERGBP = curl_multi_getcontent($chUSERGBP);
curl_multi_close($mh);
$conversionResultGBPUSD = json_decode($GBPUSD, true);
$conversionResultUSERGBP = json_decode($USERGBP, true);
$GBPUSDresults = $conversionResultGBPUSD['result'];
$USERGBPresults = $conversionResultUSERGBP['result'];
$ppp = $results->ppp_conversion;
$business_plus = ( 65 * $GBPUSDresults * $ppp * $USERGBPresults );
echo '£'.number_format((float)$business_plus, 2, '.', '').' per year';
die();
endif;
}
add_action('wp_ajax_nopriv_business_plan_calculation', business_plan_calculation);
add_action('wp_ajax_business_plan_calculation', business_plan_calculation);
function enterprise_calculation() {
if(isset($_POST['countryID'])) :
$parentCat=$_POST['countryID'];
global $wpdb;
$results = $wpdb->get_row( "SELECT currency_alpha, ppp_conversion FROM master_country WHERE country='$parentCat'" );
$endpoint = 'convert';
$access_key = '3a8c6c408b87705bde661d3d17b938ed';
$user_currency = $results->currency_alpha;
$chGBPUSD = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&from=GBP&to=USD&amount=1');
$chUSERGBP = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&from='.$user_currency.'&to=GBP&amount=1');
curl_setopt($chGBPUSD, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chUSERGBP, CURLOPT_RETURNTRANSFER, true);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $chGBPUSD);
curl_multi_add_handle($mh, $chUSERGBP);
$active = null;
do {
curl_multi_exec($mh, $active);
} while ($active);
$GBPUSD = curl_multi_getcontent($chGBPUSD);
$USERGBP = curl_multi_getcontent($chUSERGBP);
curl_multi_close($mh);
$conversionResultGBPUSD = json_decode($GBPUSD, true);
$conversionResultUSERGBP = json_decode($USERGBP, true);
$GBPUSDresults = $conversionResultGBPUSD['result'];
$USERGBPresults = $conversionResultUSERGBP['result'];
$ppp = $results->ppp_conversion;
$enterprise = ( 120 * $GBPUSDresults * $ppp * $USERGBPresults );
echo '£'.number_format((float)$enterprise, 2, '.', '').' per year';
die();
endif;
}
add_action('wp_ajax_nopriv_enterprise_calculation', enterprise_calculation);
add_action('wp_ajax_enterprise_calculation', enterprise_calculation);
答案 0 :(得分:1)
这实际上很容易实现。
更改您的AJAX PHP函数以返回BOTH值,并返回JSON编码数组。
然后,在你的javascript AJAX成功回调中,获取两个数据,并将它们放入两个单独的输入中。
以下更改被评论以解释发生了什么:
File -> Invalidate Caches/Restart -> Invalid and Restart
注意强>: 在完成问题中的代码之后,会出现很多拼写错误和其他问题。问题不在于修复/改善这些问题,所以我一直坚持向模式展示如何做你想做的事情。如果它不适合您,则需要解决代码中存在的问题。