Magento Forum

   
Magento - Ajax calling PHP not working
 
joequah1
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`.

<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>

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.

<?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);
     
}

  ?>

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
 
bharath chinna
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2012-04-06
Chennai
 

Hi,

Please use FIREBUG developer tool to find whether request goes to test.php or not?

 
Magento Community Magento Community
Magento Community
Magento Community
 
joequah1
Jr. Member
 
Total Posts:  4
Joined:  2013-04-07
 

Thank you bharath_chinna

After I check, I found out that the location is incorrect. So I provided the full url and it works. Thank you.

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