Magento Enterprise – How to Escape PHP Tags in JavaScript File

javascriptmagento-enterprisePHP

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

Best Answer

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.

Related Topic