Pow
Pow

Reputation: 1367

Cannot use php tag in javascript tag

I'm trying to use php tag in a javascript function. But unfortunately its not working. I even used <?php but still not working. I checked php.ini file and there the short tag is already enabled.

Any sort of suggestion will be appreciated.

Upvotes: 1

Views: 1740

Answers (2)

Thariama
Thariama

Reputation: 50832

You should be aware that a php tag inside javascript gets evaluated on the serverside before it gets sent to the client. Example: If the php variable $feeling holds the string "love" "I love cheese." gets alerted.

<script type="text/javascript">
var text = "I <?php echo $feeling;?> cheese";
alert(text);
</script>

In that case the javascript code that gets returned from the server will look like:

<script type="text/javascript">
var text = "I love cheese";
alert(text);
</script>

I hope this will be of help to you to solve your issue.

Upvotes: 5

Chandresh M
Chandresh M

Reputation: 3828

as you described here, your code should be working. you can use php code in JavaScript with PHP tags() if you are using core php.but its not working in smarty structure. so please mind it and check if you are using any framework or structures.

Thanks.

Upvotes: 0

Related Questions