Reputation: 35
I am trying to append php script to the dom in a div that has the class .new-session but it doesn't work how it should.
Here is my code
$('select[name=quantity]').change(function() {
quantity = $(this).val();
if (quantity == 20) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 20; ?>';
}
elseif(quantity == 30) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 30; ?>';
}
elseif(quantity == 40) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 40; ?>';
}
elseif(quantity == 50) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 50; ?>';
}
elseif(quantity == 60) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 60; ?>';
}
$.ajax({
url: "/multisite/",
dataType: "html",
success: function(newSession) {
$('.set-session').append(newSession);
}
})
});
<div class="set-session">
</div>
Upvotes: 0
Views: 32
Reputation: 781098
You can't append PHP to the DOM, PHP code is run on the server when the page is being created. You use AJAX to run server code in response to a user action, and the script that receives the AJAX call updates the session.
Here's how you can do what you want:
$('select[name=quantity]').change(function() {
var quantity = $(this).val();
$.ajax({
url: "/multisite/setquantity.php",
data: { quantity: quantity }
});
});
The PHP in setquantyt.php
should be like:
<?php
session_start();
$_SESSION['product']['quantity'] = intval($_GET['quantity']);
Upvotes: 1