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

时间:2017-10-26 17:06:50

标签: javascript php mysql ajax magento

我是ajax的新手,所以我不确定我是否正确接近这个,基本上我在javascript中有一个需要插入数据库的变量,这是我到目前为止所拥有的... < / p>

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,我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

//请将您的insert.php文件放在根路径(Magento安装路径)中,并在您的javascript代码中更改以下行。

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