变量没有在php中成倍增加

问题描述 投票:1回答:2

$newtotal没有在php中成倍增加。

我希望$newtotal被回显,但它只回显数字0.我希望能够获得跨度类total-cart的值并让它在php中成倍增加。它不会因为javascript而放弃。我在php下面添加了所有的javascript信息。

PHP

<span class= "total-cart"></span>
<?php 
 echo '<span class="total-cart"></span>'; 
 echo $total-cart;
 $newtotal= $total-cart*3;    
 echo $newtotal;
?>

JS

<script>
// Shopping Cart API
var shoppingCart = (function() {
// Private methods and propeties
cart = [];

// Constructor
function Item(name, price, count) {
  this.name = name;
  this.price = price;
  this.count = count;
}

// Save cart
function saveCart() {
  sessionStorage.setItem('shoppingCart', 
JSON.stringify(cart));
}

// Load cart
function loadCart() {
cart = JSON.parse(sessionStorage.getItem('shoppingCart'));
}
if (sessionStorage.getItem("shoppingCart") != null) {
  loadCart();
}

// Public methods and propeties
var obj = {};

// Add to cart
obj.addItemToCart = function(name, price, count) {
for(var item in cart) {
  if(cart[item].name === name) {
    cart[item].count ++;
    saveCart();
    return;
  }
}
var item = new Item(name, price, count);
cart.push(item);
saveCart();
}
// Set count from item
obj.setCountForItem = function(name, count) {
for(var i in cart) {
  if (cart[i].name === name) {
    cart[i].count = count;
    break;
  }
}
};
// Remove item from cart
obj.removeItemFromCart = function(name) {
  for(var item in cart) {
    if(cart[item].name === name) {
      cart[item].count --;
      if(cart[item].count === 0) {
        cart.splice(item, 1);
      }
      break;
    }
  }
  saveCart();
}

// Remove all items from cart
obj.removeItemFromCartAll = function(name) {
for(var item in cart) {
  if(cart[item].name === name) {
    cart.splice(item, 1);
    break;
  }
  }
saveCart();
}

// Clear cart
obj.clearCart = function() {
cart = [];
saveCart();
}

// Count cart 
obj.totalCount = function() {
var totalCount = 0;
for(var item in cart) {
  totalCount += cart[item].count;
}
return totalCount;
}

// Total cart
obj.totalCart = function() {
var totalCart = 0;
for(var item in cart) {
  totalCart += cart[item].price * cart[item].count;
}
return Number(totalCart.toFixed(2));
}

// List cart
obj.listCart = function() {
var cartCopy = [];
for(i in cart) {
  item = cart[i];
  itemCopy = {};
  for(p in item) {
    itemCopy[p] = item[p];

  }
  itemCopy.total = Number(item.price * 
item.count).toFixed(2);
  cartCopy.push(itemCopy)
}
return cartCopy;
}

// cart : Array
// Item : Object/Class
// addItemToCart : Function
// removeItemFromCart : Function
// removeItemFromCartAll : Function
// clearCart : Function
// countCart : Function
// totalCart : Function
// listCart : Function
// saveCart : Function
// loadCart : Function
return obj;
})();

// Triggers / Events
// Add item
$('.add-to-cart').click(function(event) {
event.preventDefault();
var name = $(this).data('name');
var price = Number($(this).data('price'));
shoppingCart.addItemToCart(name, price, 1);
displayCart();
});

// Clear items
$('.clear-cart').click(function() {
shoppingCart.clearCart();
displayCart();
});

function displayCart() {
var cartArray = shoppingCart.listCart();
var output = "";
for(var i in cartArray) {
output += "<tr>"
  + "<td>" + cartArray[i].name + "</td>" 
  + "<td>(" + cartArray[i].price + ")</td>"
  + "<td><div class='input-group'><button class='minus- 
item input-group-addon btn btn-primary' data-name=" + 
cartArray[i].name + ">-</button>"
  + "<input type='number' class='item-count form- 
control' data-name='" + cartArray[i].name + "' value='" 
+ cartArray[i].count + "'>"
  + "<button class='plus-item btn btn-primary input- 
group-addon' data-name=" + cartArray[i].name + ">+</button></div></td>"
  + "<td><button class='delete-item btn btn-danger' data-name=" + cartArray[i].name + ">X</button></td>"
  + " = " 
  + "<td>" + cartArray[i].total + "</td>" 
  +  "</tr>";
}
$('.show-cart').html(output);
$('.total-cart').html(shoppingCart.totalCart());
$('.total-count').html(shoppingCart.totalCount());
}

// Delete item button
$('.show-cart').on("click", ".delete-item", 
function(event) {
var name = $(this).data('name')
shoppingCart.removeItemFromCartAll(name);
displayCart();
})


// -1
$('.show-cart').on("click", ".minus-item", function(event) {
var name = $(this).data('name')
shoppingCart.removeItemFromCart(name);
displayCart();
})
// +1
$('.show-cart').on("click", ".plus-item", function(event) {
 var name = $(this).data('name')
shoppingCart.addItemToCart(name);
displayCart();
})

// Item count input
$('.show-cart').on("change", ".item-count", function(event) {
var name = $(this).data('name');
var count = Number($(this).val());
shoppingCart.setCountForItem(name, count);
displayCart();
});

displayCart();
</script>
javascript php html class echo
2个回答
1
投票

如果你想让PHP做数学运算javascript用于创建初始值,你需要查看AJAX帖子以及如何处理它们。一个例子可能如下:

JS(为了简单起见JQUERY)

$.post('path/to/your/phpscript.php', {type: "multi", value: cartvalue}, function(returned) {
  console.log(returned);  //Do whatever you need to do with the value now.
});

PHP AJAX句柄

  $returnthis;
  switch($_POST['type']) {
    case "multi":
         //Do your multiplication here.  Your initial value is stored in $_POST['value'].
    //ie - $returnthis = $_POST['value'] * 3;
    break;
  }
  return $returnthis;

0
投票

cart在PHP中不可用(PHP无法读取JS变量)。

你可以做的是把$newTotal放在一个隐藏的领域。然后使用你的JS来获取隐藏字段的值并进行计算并使用JS将其打印到屏幕上。

PHP

<span class= "total-cart"></span>
<?php 
 echo '<span class="total-cart"></span>'; 
 echo $total-cart;
 $newtotal= $total-cart*3;    
 echo '<input type="hidden" id="newTotal" value="'.$newtotal.'"/>';
 echo '<span id="output"></span>';
?>

JS

document.getElementsById('output').innerText = parseInt(document.getElementsById('newTotal'), 10) - cart * 3;

刚刚注意到你使用jQuery,你也可以这样做

$('#output').text(parseInt($('#newTotal').value(), 10) - cart * 3);

注意您需要使用parseInt将#newTotal中的值从字符串转换为Integer

© www.soinside.com 2019 - 2024. All rights reserved.