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 magento.stackexchange.com, 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 community@magento.com.

Magento Forum

Using JSON in phtml file
 
gougeaway
Jr. Member
 
Total Posts:  4
Joined:  2008-07-30
 

I’m struggling with a CMS page.  I’m using:

{{block type="core/template" template="locations/storelist.phtml"}}

to create a list of shop locations.  This works fine, it includes the file, and on this file I grab a list of shop provinces from a table in the database.

I have a drop down for PROVINCE, and a drop down for city.

When I change the CITY dropdown, I’d like it to read a phtml file using JSON in ajax .  The file is getcities.phtml and is in the same directory as storelist.phtml. 

If I php include the getcities.phtml file it works fine, it reads the $_GET[’province’] and pulls a list of cities in that province. 

With a few adjustments, I’m using the jquery code from:

http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/

My javascript looks like this:

$(function(){
  
$("select#province").change(function(){
    
$.getJSON("getcities.phtml",{id: $(this).val(), ajax'true'}, function(j){
      
var options '';
      for (var 
0j.lengthi++) {
        options 
+= '<option value="' j[i].optionValue '">' j[i].optionDisplay '</option>';
      
}
      
$("select#city").html(options);
    
})
  
})
})

Let’s assume the above code is correct (I may have to change the id: $this.val() to province: $this.val()) Using Firebug I’ve determined I’m getting a 404 error on the .getJSON("getcities.phtml") call - since it’s looking for it at http://www.example.com/getcities.phtml)

Is there a workaround for this?  How can I access getcities.phtml using JSON.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Do you need to read just a PHTML file as if it was an AJAX call?

If so, you ideally would accomplish this using a controller, a block and a template(.phtml), but you can get away with this with just a block and template.

To run code outside of magento (something I have used to run ajax calls) :

<?php
require_once 'app/Mage.php';
umask(0);
//not Mage::run();
Mage::app('default');

//code snipped
$className Mage::getConfig()
            ->
getBlockClassName('catalog/product_ajax');

$block = new $className();

$className Mage::getConfig()
            ->
getBlockClassName('core/template');

$toolbar = new $className();
$block->setChild('toolbar'$toolbar);

//choose whatever category ID you want
$block->setCategoryId(3);
$block->setTemplate('catalog/product/ajaxtemplate.phtml');

echo 
$block->renderView();

?>

You can plop that in your root directory (same folder as your index.php and .htaccess) and call this php file via an ajax call

 
Magento Community Magento Community
Magento Community
Magento Community
 
gougeaway
Jr. Member
 
Total Posts:  4
Joined:  2008-07-30
 

Thanks, this seems to do the trick, I know it’s not the idea way of running things, but now that I’ve got it working this way I’ll see if I can get it working the way it ought to be working.  I posted a question in the Programming forum to find out the best way to get started doing things the right way, but got no responses, sadly.

Thanks again.

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