显示来自php文件

时间:2017-11-05 05:26:07

标签: javascript php

我有购物车的折扣代码,计算扣除%10,一切正常,但我想将折扣金额($discount_amount)显示为减值( - £2.50)或显示£0.00如果没有应用折扣,只是不确定如何最好的方式,任何建议非常感谢。

page.php文件:

$currency = '£';

if(isset($_POST['action']) && $_POST['action'] == "coupon_dis")
{
    $couponcode = trim($_POST['couponval']);
    $amnt = $_POST['amnt'];
    $val = '0';
    if($couponcode == "loyalty10"){
        $couponDiscount_val = "10";
        $discount_amount = ($amnt*$couponDiscount_val)/100;
        $amount_after_discount = $amnt - $discount_amount;
        $val = sprintf("%01.2f", $amount_after_discount);
    }
    $_SESSION['discount_amount'] = sprintf("%01.2f", $discount_amount);
    $_SESSION['product_discount'] = $val;
    echo $currency.$val;
}

javascript:

$(document).ready(function(){
$("#addcoupon").click(function(){
    var ttlamt = $("#ttl_amount").val();
    var ccode = $("#couponcode").val();
    var cpn = ccode.replace(/\s/g, '');
    if(cpn.length <= 0){
        $("#couponcode").css("border-color","red");
    }else{
      $("#couponcode").css("border-color","");
      $.ajax({
          url: "page.php",
          method: "POST",
          data: {action:"coupon_dis",couponval: cpn,amnt: ttlamt},
          cache: false,
          success: function(data){
            if(data != "0"){
                $("#grandamount").css("display","none");
                $("#couponcode").css("border-color","green");
                $("#grand_discount_amount").css("display","block");
                $("#grand_discount_amount").html(data);
            }else{
              $("#grandamount").css("text-decoration","");
              $("#couponcode").css("border-color","");
              $("#grand_discount_amount").html("");
              $("#grand_discount_amount").css("display","none");
            }
          }
      });
    }
    return false;
});

2 个答案:

答案 0 :(得分:0)

gmp_neg(“$ var”)应该将值转换为负值。但这种变化对消极和负面变为正面。你能不能将$ discount_amount乘以(-1)? 0 *( - 1)= 0 ... 如果要在同一页面上显示它,可以使用ajax(您使用过),但是如果要在更多数组中拆分php的结果,可以使用json_encode_array。或者你可以在$ _SESSION ...

中存储你需要的东西

答案 1 :(得分:0)

您可以从PHP返回多个值:

let asset = AVAsset(url: videoURL)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
videoLooper = AVPlayerLooper(player: player, templateItem: item)

现在您可以在Javascript中使用这些值:

// ... your code

$return = [
    'price' => $currency.$val
    'discount' => $currency.$discount_amount
];

// Make sure Javascript knows you're returning JSON data
header('Content-Type: application/json');
echo json_encode($return);