使用Ajax显示php的百分比
问题描述:
对不起。 我试图让这个过程不起作用。 我有这样的代码:使用Ajax显示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);
看到的结果我已经把建立一个数字:
$receipt = paypalFees('150.00', false);
结果由
<h2>Your Receipt</h2>
<li>Sub-Total: £<?php print $receipt['sub_total']; ?></li>
<li>Booking Fee: £<?php print $receipt['paypal_fee']; ?></li>
<li>TOTAL: £<?php print $receipt['grand_total']; ?></li>
所示看到的结果我有刷新页面。
如何在没有刷新页面的情况下使用ajax来执行此过程? 一个例子: Sum HTML Textbox Values using jQuery/JavaScript
答
不知道你的应用程序的工作流程任何这里是我最好的猜测:
- 把你的代码出来的功能。
- 与普通的HTML元素让你的主显示页面中的PHP页面
- 使用代码的提供和回声出值
如果你只是简单的显示结果为onload,那么你可以做到这一点我的方式。如果你需要与用户和数据库的交互,你需要AJAX。
答
如果要在不刷新页面的情况下显示数据,则必须使用JavaScript编写paypalFees()
函数。而不是返回一个数组,你只需使用JavaScript来设置跨度的内容,如下所示:
<h2>Your Receipt</h2>
<li>Sub-Total: £<span id="receipt_subtotal"></span></li>
<li>Booking Fee: £<span id="paypal_fee"></span></li>
<li>TOTAL: £<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电话:下面Javascript对象
<h2>Your Receipt From @Ramsay Smith (thx)</h2>
<li>Sub-Total: £<span id="receipt_subtotal"></span></li>
<li>Booking Fee: £<span id="paypal_fee"></span></li>
<li>TOTAL: £<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>
答
修改
使用来实现这一目标,根据您的需要
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();' >
查看网络套接字,例如http://pusher.com – Utkanos 2014-10-30 13:12:45
您既可以使用AJAX,也可以使用Javascript进行所有计算。 – Barmar 2014-10-30 13:13:25