1

我已经建立了一个基本的 SimpleCart.js 网上商店,并且正在尝试实施realseanp 的解决方案以添加促销/折扣代码功能。

他使用以下 JavaScript 将用户输入的促销代码发布到 discount.php:

jQuery("#promoSub").click(function Discount() {
    //Interact with PHP file and check for valid Promo Code
    jQuery.post("discount.php", { code: jQuery('#code').val() } , function(data) {
        console.log(jQuery('#code').val());
        if (data=="0" || data=='') { 
            console.log("Wrong Code"); 
        }
        else {
            //create our cookie function if promo code is valid
            //create cookie for 1 day
            createCookie("promo","true",1);
            var y = (data/100);
            for(var i=0; i<simpleCart.items().length; i++){
                var itemPrice = simpleCart.items()[i].price();
                var theDiscount = itemPrice * y;
                var newPrice = itemPrice - theDiscount;
                simpleCart.items()[i].set("price", newPrice)
            }
            simpleCart.update();
            //hides the promo box so people cannot add the same promo over and over
            jQuery('#promoCodeDiv').hide();

        }
    });
});

...如果已通过正确的代码,则返回 0 或折扣百分比:

<?php
$x = 0;
if ($_POST["code"]=="HOLIDAY") { $x = 5; };
echo $x;
?>

但是,当我在成功函数中使用 console.log(data) 时,数据似乎是整个 PHP 脚本作为字符串而不是回显。

为什么会这样,我需要进行哪些修改来纠正它?我对 PHP 很陌生,很难理解为什么 jQuery.post() 没有收到回声。

4

0 回答 0