2017-06-17 34 views
3

国に応じて価格を計算するページを作成しようとしています。私は2つのプライシング層を持ち、国を選択すると2つの別々の入力フィールドで価格を更新したいと考えています。私はJQueryとbusiness_plan_calculation関数を2回繰り返すことでこれを行うことができましたが、1つの関数でそれを行う方法があるかどうかを知りたがっていますか?ここでPHP関数から複数の入力ボックスにデータを入力する

は、私はこれは実際に達成するために非常に簡単です二つの機能

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); 

答えて

1

でそれを行うために使用していたコードです。

BOTH値を返すようにAJAX PHP関数を変更し、JSONエンコードされた配列を返します。

次に、JavaScriptのAJAX成功コールバックで、2つのデータを取得し、2つの別々の入力に入れます。以下

変更は何が起こっているのかを説明するためにコメントしています

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>'; 
    } ?> 
<script> 
    // no-conflict safe shorthand document ready 
    jQuery(function($) { 
     // now $ is safe to use inside this function 
     $('#country_selector').change(function() { 
      var countryPOP = $('#country_selector').val(); 

     $.ajax({ 
      // use the WP function admin_url() here... 
      url: '<?php echo admin_url('admin-ajax.php') ?>', 
      type: 'POST', 
      // tell jQuery we expect JSON back so it auto-parses to JSON 
      dataType: 'json', 
      data: 'action=country_calculations&countryID=' + countryPOP, 
      success: function(results) { 
       // NOTE: Should do something here if results is NOT what is expected 
       // clear BOTH inputs 
       $('#business_plus_price, #enterprice_price').empty(); 
       // access the business_plus results, put into input 
       $('#business_plus_price').val(results.business_plus); 
       // access the enterprise results, put into input 
       $('#enterprise_price').val(results.enterprise); 
        } 
       }); 
     }); 
</script> 
<?php  
    return '<select id="country_selector"><option value="0" disabled selected>Select Your Country</option>'.$option.'</select>'; 

} 

// new AJAX function that gets and returns BOTH price calculations 
function ajax_country_price_calculations() { 
    $data = country_price_calculations(); 
    // NOTE: should do something here in case $data is empty/FALSE 

    // this function json_encodes, outputs, and dies 
    wp_send_json($data); 
} 

// SIMPLIFIED your two functions into a single function. 
// This reduces duplicate code, AND reduces CURL calls. 
function country_price_calculations() { 

    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); 
     $enterprise = (120 * $GBPUSDresults * $ppp * $USERGBPresults); 
     // RETURN the results instead of echo now... 
     // assign calculations to an associative array 
     return array(
      'business_plus' => '£'.number_format((float)$business_plus, 2, '.', '').' per year', 
      'enterprise' => '£'.number_format((float)$enterprise, 2, '.', '').' per year' 
     ); 
    } 
    // NOTE: Should return SOMETHING (such as FALSE) if this fails... 
} 

// Now you only need one AJAX endpoint 
add_action('wp_ajax_nopriv_country_price_calculations', 'ajax_country_price_calculations'); 
add_action('wp_ajax_country_price_calculations', 'ajax_country_price_calculations'); 

: 問題のコードを働いた後、多くのタイプミスや他の問題があります。問題はそれらの問題を修正/改善することではないので、あなたが望むことを行う方法についてパターンを提示することに固執しました。それが機能しない場合は、コードに存在する問題をトラブルシューティングする必要があります。

+0

ありがとうございました。私はこれを実装しようとしましたが、動作させることができません。私はおそらくそれはおそらくなぜコーディングに新しいです。私の元のコードには、タイプミスやその他の問題がありました。私に例を挙げてください。私の元のコードはうまくいきましたので、どこでエラーを見つけるのか分かりません。ありがとう – Dudley

+0

たとえば、次の行は機能しませんでした。 'add_action( 'wp_ajax_nopriv_enterprise_calculation'、enterprise_calculation);' - 第2引数は文字列または配列である必要があります。 –

+0

これをありがとう、私は今それが働いている – Dudley

関連する問題