我在local
中安装了Magento。
我将自己的Ajax代码magento\app\design\frontend\base\default\template\catalog\product\view.phtml
Magento - Ajax调用PHP不工作
我试图在同一位置张贴一些变量到另一个PHP文件为view.phtml
。
<script>
function loadXMLDoc()
{
var user = "<?php echo $userId;?>";
var product = "<?php echo $xxx;?>";
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","test.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("userID="+user+"&productID="+product);
}
//this is just a countdown timer
var timeLeft = 3;
var timer = window.setInterval(function() {
timeLeft--;
var minutesLeft = Math.floor(timeLeft/60);
var secondsLeft = timeLeft % 60;
console.log('Time left: ' + minutesLeft + ':' + secondsLeft);
if (timeLeft == 0) {
window.clearInterval(timer);
// do some ajax thing
loadXMLDoc();
}
}, 1000);
</script>
基本上我会在几秒后发布2个变量到test.php。 我很确定这些代码正在工作,但是当我在magento产品页面中尝试它们时。 它不工作。我试图确定,如果所有的代码正在执行。 一切运行良好,只有Ajax不会在PHP文件中发布。
在test.php文件中,我只是得到2个变量并将其保存到位于同一文件夹中的.txt文件中。 test.php文件仅用于测试目的。
我需要的只是让Ajax工作。我希望它发布到test.php,但保持在同一页面上。
我谷歌周围,找到了一些解决方案,它似乎有一些与Magento控制器或一些连接。但我无法理解它。
任何人都可以请告诉我,如果有一个简单的方法来解决这个问题?
我只是为了使事情更清楚。
<?php
if(isset($_POST['userID']))
{
$userID = $_POST['userID'];
$productID = $_POST['productID'];
$file = "db.txt";
$current = file_get_contents($file);
$current .= "\n$userID \t $productID";
file_put_contents($file, $current);
}
?>
和db.txt放在同一文件夹中的test.php
如何知道是否test.php的被称为? 我试图检查db.txt,但里面没有任何改变。
修正
我把我的test.php和db.txt根。 和更改
xmlhttp.open("POST","test.php",true)
到
xmlhttp.open("POST","http://localhost/magento/test.php",true)
THANK YOU
你一定'test.php'存在于你的Magento根?例如'test.php'应该存在'index.php'旁边 –
嗨Jeremi,test.php与view.phtml位于同一个文件夹。或者我应该把test.php放在根目录下?我把txt文件放在根目录和产品文件夹中。谢谢。 –