1

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.

2
  • 4
    You should post the relevant code. Commented Nov 29, 2010 at 6:31
  • 2
    And describe the difference between "What happens" and "What you expect". Just saying "its not working" is uninformative. Commented Nov 29, 2010 at 6:36

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.