-4

I have am currently working on a web shop. The website has a small basket in the top right that shows the current price of all the products in the basket. It shows the correct number, however only updates the number after a refresh of the page, or after you navigate to another place on the website.

I have a file called basket.php which correctly calculates the sum of the products and puts it in a variable called $sum. There is also a file called tinybasket.php, which is only for showing the small basket in the top right.

Basket.php uses this code:

$_SESSION['sum'] = $sum;

tinybasket.php has this code:

echo $_SESSION['sum']   

How do i update the tinybasket in the top right, without the user having to refresh or navigate to another page?

jrswgtr
  • 2,287
  • 8
  • 23
  • 49
CodeMan
  • 85
  • 5
  • 2
    use ajax to get it done –  Nov 14 '20 at 10:41
  • 2
    There are many duplicate questions here on SO, with code you can use and learn from, have you tried any of them? A few examples: https://stackoverflow.com/questions/12839571/update-cart-with-jquery-ajax, https://stackoverflow.com/questions/27854730/php-updating-cart-total-amount-without-page-refresh, https://stackoverflow.com/questions/18454560/refresh-session-and-array-without-refreshing-page, https://stackoverflow.com/questions/29010176/submit-form-without-redirect-to-action-page-but-refresh-current-page – Don't Panic Nov 14 '20 at 11:00

1 Answers1

2

You need Javascript to go on. Because PHP is rendering your tinybasket.php into HTML code which means in browser there is no PHP variable it is the only text.

Altantur
  • 2,552
  • 1
  • 13
  • 12