Update Product Price in Session with jQuery and PHP for Dynamic Pricing
To update the price in the session using jQuery when the price changes in HTML, you can follow these steps:
-
When the price changes in HTML using jQuery, send an AJAX request to a PHP file that will handle the update in the session.
-
In your jQuery code, use the
$.ajax()method to send the AJAX request. You can trigger this request on the change event of the price input field.$('#priceInput').on('change', function() { var newPrice = $(this).val(); $.ajax({ url: 'update_session.php', method: 'POST', data: { price: newPrice }, success: function(response) { console.log('Price updated in session'); } }); }); -
In the PHP file (
update_session.phpin this example), retrieve the new price from the AJAX request and update it in the session.<?php session_start(); if(isset($_POST['price'])) { $newPrice = $_POST['price']; $_SESSION['product_price'] = $newPrice; echo 'Price updated in session'; } ?> -
Make sure to start the PHP session at the beginning of your PHP files where you need to access or update the session data.
<?php session_start(); // Rest of your PHP code...
Now, whenever the price changes in the HTML, the AJAX request will be triggered and the PHP file will update the price in the session.
原文地址: https://www.cveoy.top/t/topic/qc65 著作权归作者所有。请勿转载和采集!