dongshi3361 2015-10-29 01:03
浏览 50

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

I've already set up a basic SimpleCart.js web shop and am trying to implement realseanp's solution for adding a promo/discount code feature.

He uses the following JavaScript to post the user's entered promo code to 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();

        }
    });
});

...which echoes back a 0 or the discount percentage if the correct code has been passed:

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

However, when I console.log(data) from within the success function, data appears to be the entirety of the PHP script as a string instead of the echo.

Why is this, and what modifications do I need to make to rectify it? I'm very new to PHP and having difficulty understanding why jQuery.post() isn't receiving the echo.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题