1

I'm busy doing server side and client side validation for magento. this validation works fine on server side (php)

on the client side i am using javasrcript.

When i started on this. i had my javascript embedded on a phtml file and everything was working as expected.

because i am using magento so I decided to inject the javascript file via page.xml

When I added the javascript code instead of getting the message pulled I get the php as is.

Here is my javascript:

function DefaultAddressErrorChangeNotAllowedMessage() {
alert("<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>");
return;

}

I run this when a user hit the onclick it will point to this function DefaultAddressErrorChangeNotAllowedMessage() and the

 <?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>

will be populated as is.

but when I embed this directly to a phtml file it pull the correct message.

Is there a way for javasrcipt that I can use to escape the php and get the correct message which is pulled from config.xml

2 Answers 2

3

You cannot use php tags inside javascript files.
Either you change your js file to this:

function DefaultAddressErrorChangeNotAllowedMessage() {
    alert(defaultAddressErrorMessage);
    return;
}

and define in the <head> tag this:

var defaultAddressErrorMessage = "<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>"

Or you out your code inside a phtml file and use it like you already do.

7
  • Well, actually you can, however you need to configure your web server / PHP to parse .js files as well. It's simple to set up, but it's by no means a standard out of the box configuration.
    – Pronto
    Jun 27, 2014 at 9:14
  • @Pronto. Huh? I never knew that. But is it a good idea to do it?
    – Marius
    Jun 27, 2014 at 9:17
  • 1
    Not really, because this is not gow PHP has been usually set up and you must have root level access to configuration files. But it's one solution and it might be acceptable way for people who know what they are doing :)
    – Pronto
    Jun 27, 2014 at 9:20
  • I will add it back to the .phtml. I was going to use ajax though but i'm not that pro with it Jun 27, 2014 at 9:33
  • @Pronto I think this solution is better than yours :) .js files are cached by browsers and can be minified. Also you can have something like Varnish in front of the web server or use a CDN to provide resources like js, css, images. And only forward requests that need php processing to the web server.
    – Emi
    Jun 27, 2014 at 9:55
2

One solution you might try out is to use .php file extension instead of .js extension for your javascript files. This way web server knows that this file must be forwarded to browser THROUGH php and thus tags get parsed.

2
  • This is a bad idea, because to do this, you have to add Mage::app() to the JS/PHP file which initializes magento and costs a lot performance Jun 27, 2014 at 13:08
  • Of course it's bad idea, but so is making ajax call as this applies to it as well.
    – Pronto
    Jun 27, 2014 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.