Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email

Magento Forum

Magento - Ajax calling PHP not working
Jr. Member
Total Posts:  4
Joined:  2013-04-07

I installed Magento in `local`. 
I added my Ajax code in `magento\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\app\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\design\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\frontend\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\base\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\default\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\template\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\catalog\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\product\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\view.phtml`

I\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’m trying to post some variables to another PHP file in the same location as `view.phtml`.

var user \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"<?php echo $userId;?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
product \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"<?php echo $xxx;?>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
          if (
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
{// code for IE6, IE5
xmlhttp=new ActiveXObject(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"Microsoft.XMLHTTP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");

//this is just a countdown timer
var timeLeft 3
timer window.setInterval(function() {
minutesLeft Math.floor(timeLeft 60);
secondsLeft timeLeft 60;
console.log(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'Time left: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' minutesLeft \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\':\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' secondsLeft);
         if (
timeLeft == 0{
// do some ajax thing

Basically I will post 2 variables to the test.php after a few seconds.
I\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’m very sure these codes are working, but when I tried them in the magento product page.
Its not working. I tried to make sure, if all the code are being executed.
Everything runs fine, only the Ajax won\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’t post in the PHP files.

In the test.php file, I just get the 2 variables and save it to a .txt file located in the same folder.
The test.php file is for testing purpose only.

All I need is to get the Ajax working. I want it to post to test.php but remain on the same page.

I Google\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’d around and found some solutions, it seems to have some connection with the Magento controller or something. But I just couldn\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’t understand it.

Can anyone please tell me if there\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s a simple way to fix this problem?


I add this just to make things more clear.


$productID $_POST[\\\\\\\'productID\\\\\\\'];

$file \\\\\\\"db.txt\\\\\\\";

$current file_get_contents($file);

$current .= \\\\\\\"\\\\\\\\n$userID \\\\\\\\t $productID\\\\\\\";



and the db.txt is put on the same folder as test.php

How to know if the test.php is being called? I tried to check to the db.txt, but nothing changed inside.

Both test.php and db.txt are placed in the root and in the same folder as view.phtml.

Magento Community Magento Community
Magento Community
Magento Community
Indies Inc
Sr. Member
Total Posts:  193
Joined:  2013-09-18

Just keep in mind that Magento uses MVC structure. So when there are two files into the same folder, they cannot be accessed as two same php file into same folder for simple example. Any file into Magento can be accessed based on its full path.

So for your new file, create one URL to access that file. This can be done making entry of new file into XML file, then pass complete URL into AJAX code. I know implementing AJAX into simple PHP example is different than implementing AJAX into Magneto.

I hope this will help you to solve problem.

Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top