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. It works fine in Chrome but when I’m testing the same feature in firefox,, it is behaving so strange. It is giving me around 20%, 50% discount. Like some random maths calculations.
I’m using jquery, php, mysql to show the results in real time to customer. 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. But same prob occurs. I’m so confused at this point.
Here is the jquery code:
<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:
<?
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. One is the coupon code which is set to ‘coup’ and one is the total calculated amount which is set to ‘disc’. Then both values process into different php pages and prints the results.
I think the problem isn’t with PHP code. The prob is with my jquery code. I’ve tried to use some constant values in php code to make calculations like 200-100 and it works fine. But the value of ‘disc’ variable is making a difference.
Problem solved: What was actually happening that I had included an space like this $200
This space was creating all the problem in firefox browser. So, I was using substr() initially was breaking the string from position 1 but now. I changed the breaking position and set it to 2 and everything is fine now. BTW, thanks for your help guys 🙂
PHP is a server-side language. A request is sent to the server and the server runs the PHP code. This means that no matter what browser you have, your PHP code simply CAN NOT return a different result. Same thing goes for your MySQL database. Knowing this, we can pinpoint the problem.
The problem is in your HTML or javascript (jQuery). Without code, we cant tell where.
After reading your edit:
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.
If not, we know that the data the browsers sends is different. In both cases, the bug will most likely be in your javascript.
This is the best answer you’ll get with this little information.