简体   繁体   English

不同浏览器中的不同数学计算

[英]Different math calculations in different browsers

I've created a discount feature for one of my client. 我为一个客户创建了折扣功能。 Whenever, a selected coupon is entered into the coupon fiedld, then it will match it from the database that either the coupon is active and does it exist? 每当将选定的优惠券输入优惠券字段时,它就会从数据库中匹配该优惠券是否处于活动状态并且存在吗? If yes, then it does some math calculations of giving the user 10% of discount and shows the user, the discounted amount. 如果是,则进行一些数学计算,为用户提供10%的折扣,并向用户显示折扣金额。 It works fine in Chrome but when I'm testing the same feature in firefox,, it is behaving so strange. 它在Chrome中可以正常工作,但是当我在Firefox中测试相同功能时,它的表现就太奇怪了。 It is giving me around 20%, 50% discount. 它给了我约20%,50%的折扣。 Like some random maths calculations. 像一些随机数学计算。

I'm using jquery, php, mysql to show the results in real time to customer. 我正在使用jquery,php,mysql向客户实时显示结果。 This problem is really teasing me. 这个问题真的使我开心。 Initially, I was using PHP sessions to get the discount value but I tried to store the discount value in db and then retrieving it again for calculation. 最初,我使用PHP会话来获取折扣值,但我尝试将折扣值存储在db中,然后再次对其进行检索以进行计算。 But same prob occurs. 但是同样的概率发生了。 I'm so confused at this point. 我现在很困惑。

Here is the jquery code: 这是jQuery代码:

<script type="text/javascript">
$(document).ready(function(){
 $("button").click(function(){
    $.post('coupapply.php', {coup:$('#coupon').val()}, function(result) {
    });
    $.post('discount.php', {disc:$('#totamt tr').find("td").eq(1).html()}, function(result) {
    $('#cpresult').html(result);
    });
 });
});
</script>
<span id='cpresult'></span>

Here is the php code: 这是PHP代码:

<?

if ($_REQUEST) {
$disc = $_REQUEST['disc'];
$amt = substr($disc, 1);
$caldisc = $amt*0.1;
$total = (200-$caldisc);
echo "<spain style='font-size:14px;'>*10% discount applied* Pay ".$total." now!</span>";
}
?>

Let me explain this code. 让我解释一下这段代码。 The reason I used two $post functions is because I've get two values from the current page. 我使用两个$ post函数的原因是因为我从当前页面获得了两个值。 One is the coupon code which is set to 'coup' and one is the total calculated amount which is set to 'disc'. 一个是设置为“优惠券”的优惠券代码,一个是设置为“ disc”的总计算金额。 Then both values process into different php pages and prints the results. 然后,这两个值将处理成不同的php页面并显示结果。 I think the problem isn't with PHP code. 我认为问题不在于PHP代码。 The prob is with my jquery code. 概率与我的jQuery代码。 I've tried to use some constant values in php code to make calculations like 200-100 and it works fine. 我试图在php代码中使用一些常量值来进行类似200-100的计算,并且工作正常。 But the value of 'disc' variable is making a difference. 但是“ disc”变量的值有所不同。

Problem solved: What was actually happening that I had included an space like this $200 解决了问题:实际上发生了什么事,我包括一个像这样的200美元的空间

This space was creating all the problem in firefox browser. 这个空间在firefox浏览器中造成了所有问题。 So, I was using substr() initially was breaking the string from position 1 but now. 因此,我最初使用substr()从位置1断开字符串,但现在。 I changed the breaking position and set it to 2 and everything is fine now. 我更改了中断位置并将其设置为2,现在一切都很好。 BTW, thanks for your help guys :) 顺便说一句,谢谢您的帮助:)

PHP is a server-side language. PHP是一种服务器端语言。 A request is sent to the server and the server runs the PHP code. 请求被发送到服务器,服务器运行PHP代码。 This means that no matter what browser you have, your PHP code simply CAN NOT return a different result. 这意味着无论您使用哪种浏览器,PHP代码都无法返回不同的结果。 Same thing goes for your MySQL database. 同样的事情适用于您的MySQL数据库。 Knowing this, we can pinpoint the problem. 知道这一点,我们就可以找出问题所在。

The problem is in your HTML or javascript (jQuery). 问题出在您的HTML或javascript(jQuery)中。 Without code, we cant tell where. 没有代码,我们无法分辨出哪里。


After reading your edit: 阅读您的编辑后:

I tried to store the discount value in db and then retrieving it again for calculation. 我试图将折扣值存储在db中,然后再次检索它以进行计算。 But same prob occurs. 但是同样的概率发生了。

Makes it sounds like something goes wrong when you are handling the response. 处理响应时听起来好像出了点问题。 If you temporarily change your PHP code so it always returns a static value, and the browsers still give a different result, you know it's your javascript interpreting the values incorrectly. 如果您临时更改PHP代码,以使其始终返回静态值,而浏览器仍给出不同的结果,则说明您的JavaScript错误地解释了这些值。

If not, we know that the data the browsers sends is different. 如果没有,我们知道浏览器发送的数据是不同的。 In both cases, the bug will most likely be in your javascript. 在这两种情况下,该错误很可能都在您的javascript中。

This is the best answer you'll get with this little information. 这是您获得的少量信息的最佳答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM