繁体   English   中英

Magento通过Ajax将数据插入数据库

[英]Magento insert data into database through ajax

我是ajax的新手,所以我不确定我是否正确处理此问题,基本上我在javascript中有一个变量需要插入数据库中,这就是我到目前为止所拥有的...

onInit: function() {
    window.fcWidget.on('widget:loaded', function() {
        window.fcWidget.user.get().then(function(resp) {
            var status = resp && resp.status,
            data = resp && resp.data;
            if (status === 200) {
                if (data.restoreId) {
                    // Update restoreId in  database

                    $.ajax({
                        type: "POST",
                        url: "insert.php",
                        data: data.restoreId, 
                        success: function(data) { alert("Success"); },
                        failure: function(data) { alert("Failure"); }

                    })

                }
            }
        });
    });
}

我已将文件“ insert.php”放置在同一文件夹中,但似乎根本没有被调用...

这就是insert.php的样子

<?php
    if(Mage::getSingleton('customer/session')->isLoggedIn()){

        if(isset($_POST['data.restoreId']){
            $restoreId =$_POST['data.restoreId'];
        }

        $first = Mage::getSingleton('customer/session')->getCustomer()->getFirstname();
        $last = Mage::getSingleton('customer/session')->getCustomer()->getLastname();

        $fullName = $first . "." . $last;

        //get resource model
        $resource = Mage::getSingleton('core/resource');
        //retrieve write connection
        $writeConnection = $resource->getConnection('core_write');
        //read connection
        $readConnection = $resource->getConnection('core_read');

        $exId = $fullName;
        $resId = $restoreId;

        $testQuery = "SELECT `externalId` FROM `freshchat_user` WHERE `restoreId` = '$fullName'";

        $result = $readConnection->fetchAll($testQuery);

        if(count($result) == '0'){
            $query = "INSERT INTO `freshchat_user`(`externalId`, `restoreId`) VALUES ('$exId','$resId')";
            $writeConnection->query($query);
        }else{
            //echo "nope";
        }
    }
?>

我检查了网络选项卡,但似乎根本没有调用insert.php,我的代码有什么问题?

//请将您的insert.php文件放在根路径(Magento安装路径)中,然后在您的JavaScript代码的下面更改。

url: "www.yourwebsite.com/insert.php",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM