jQuery.post()数据以字符串形式返回整个PHP脚本?

时间:2015-10-29 01:03:47

标签: javascript php jquery web simplecart

我已经设置了一个基本的SimpleCart.js网上商店,我正在尝试实施realseanp's solution for adding a promo/discount code feature.

他使用以下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;
?>

但是,当我在success函数中调用console.log(data)时,数据似乎是the entirety of the PHP script as a string而不是echo。

为什么会这样,我需要做些什么修改来纠正它?我是PHP的新手,很难理解为什么jQuery.post()没有接收回声。

0 个答案:

没有答案
相关问题