使用Ajax显示php的百分比


Show the percentage of php with Ajax

很抱歉。我试图让这个过程对我不起作用。我有这个代码:

function paypalFees($sub_total, $round_fee) {
// Set Fee Rate Variables
$fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK)
$fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK)
// Calculate Fees
$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
if ($round_fee == true) {
    $paypal_fee = ceil($paypal_fee);
}
// Calculate Grand Total
$grand_total = ($sub_total + $paypal_fee);
// Tidy Up Numbers
$sub_total = number_format($sub_total, 2, '.', ',');
$paypal_fee = number_format($paypal_fee, 2, '.', ',');
$grand_total = number_format($grand_total, 2, '.', ',');
// Return Array
return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);

要看到结果,我必须建立一个数字:

$receipt = paypalFees('150.00', false);

显示结果

<h2>Your Receipt</h2>
<li>Sub-Total: &pound;<?php print $receipt['sub_total']; ?></li>
<li>Booking Fee: &pound;<?php print $receipt['paypal_fee']; ?></li>
<li>TOTAL: &pound;<?php print $receipt['grand_total']; ?></li>

要查看结果,我必须刷新页面。

使用ajax,在不刷新页面的情况下如何完成此过程?示例:使用jQuery/JavaScript 求和HTML文本框值

在不了解应用程序工作流程的情况下,我的最佳猜测是:

  1. 将代码从函数中取出
  2. 使您的主显示页面成为包含普通HTML元素的PHP页面
  3. 使用您提供的代码并回显值

如果你只是在加载时显示结果,那么你可以按照我的方式来做。如果您需要与用户和数据库进行交互,则需要AJAX。

如果您想在不刷新页面的情况下显示数据,则必须用JavaScript编写paypalFees()函数。您只需使用JavaScript设置跨度的内容,而不是返回数组,如下所示:

<h2>Your Receipt</h2>
<li>Sub-Total: &pound;<span id="receipt_subtotal"></span></li>
<li>Booking Fee: &pound;<span id="paypal_fee"></span></li>
<li>TOTAL: &pound;<span id="receipt_total"></span></li>

然后让JavaScript设置跨度的内容。

您可以制作这样的东西:

<?php // getpourcent.php
function paypalFees($sub_total, $round_fee) {
  // Set Fee Rate Variables
  $fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK)
  $fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK)
  // Calculate Fees
  $paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
  if ($round_fee == true) {
      $paypal_fee = ceil($paypal_fee);
  }
  // Calculate Grand Total
  $grand_total = ($sub_total + $paypal_fee);
  // Tidy Up Numbers
  $sub_total = number_format($sub_total, 2, '.', ',');
  $paypal_fee = number_format($paypal_fee, 2, '.', ',');
  $grand_total = number_format($grand_total, 2, '.', ',');
  // Return Array
  return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
}
exit(json_encode(paypalFees($_POST['sub_total'], $_POST['round_fee'])));
?>

您的功能,但具有json_encode输出。

另一个文件(客户端)在setInterval:中使用jquery getson调用

<h2>Your Receipt From @Ramsay Smith (thx)</h2>
<li>Sub-Total: &pound;<span id="receipt_subtotal"></span></li>
<li>Booking Fee: &pound;<span id="paypal_fee"></span></li>
<li>TOTAL: &pound;<span id="receipt_total"></span></li>
<script>
function getPourcent()
{
  $.getJSON('getpourcent.php', { 'sub_total': 0, 'round_fee': 0 }, function (data)
  {
    // take a look to the @Ramsay Smith code
    $('#receipt_subtotal').text(data.sub_total);
    $('#paypal_fee').text(data.paypal_fee);
    $('#receipt_total').text(data.grand_total);
  });
}
setInterval(getPourcent, 500);
</script>

使用下面的Javascript对象来实现这一点,根据您的需要对其进行修改

var generateReceipt = {
  paypalFees: function(round_fee) {
    var sub_total = $('#tb').val();
    
    var fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK)
    var fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK)
    // Calculate Fees
    var paypal_fee = (((sub_total / 100) * fee_percent) + fee_cash );
   if (round_fee == true) {
      paypal_fee = ceil(paypal_fee);
    }
    
    // Calculate Grand Total
    var grand_total = (sub_total + paypal_fee);
    // Tidy Up Numbers
    sub_total = generateReceipt.number_format(sub_total, 2, '.', ',');
    
    paypal_fee = generateReceipt.number_format(paypal_fee, 2, '.', ',');
    
    grand_total = generateReceipt.number_format(grand_total, 2, '.', ',');
    // Return Array
    return alert('Grand_Total:' + grand_total + 'Paypal_Fee:' + paypal_fee + 'Sub_Total:' + sub_total);
  },
  number_format: function(number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+'-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
      prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
      sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
      dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
      s = '',
      toFixedFix = function(n, prec) {
        var k = Math.pow(10, prec);
        return '' + Math.round(n * k) / k;
      };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
      s[0] = s[0].replace(/'B(?=(?:'d{3})+(?!'d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
      s[1] = s[1] || '';
      s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type ='text'
  name = 'fee'
  id = 'tb' >
<input type='button'
  name ='calculate' value='Calculate'
  onclick = 'generateReceipt.paypalFees();' >