1

How can i use this variable inside this?

<script language="JavaScript">document.title = "{$row['subject']} - Forum - Terraria 7.6";</script>

Tried every way

1
  • 2
    My question is why are you declaring the title in javascript anyway? Surely it should be defined in the <title> tag given that you are assigning it to a server-ide variable. Commented Jun 4, 2011 at 18:54

3 Answers 3

2
document.title = "<?php echo $row['subject'] ?> - Forum - Terraria 7.6";
Sign up to request clarification or add additional context in comments.

Comments

2

Wrap it in <?php tags:

<script type="text/javascript">
document.title = "<?php echo $row['subject']; ?> - Forum - Terraria 7.6";
</script>

Also, the language attribute is depreciated. Don't use it. Use the type instead.

Comments

1

output the var in <?php ?> tags

<script type="text/javascript">document.title = "<?php echo $row['subject'];  ?> 

- Forum - Terraria 7.6";</script>

4 Comments

language is deprecated. Please don't just copy and paste his bad code with minor additions.
Be really carefull ! The code you post leads to injections. When injecting such a var, you have to escape it. This generally a practice to avoid, so it have to be considered twice at least :D
the language attribute may be deprecated but it's not the case. It doesn't do any wrong in this case. the question was how to assign PHP variables to javascript variables and I showed how. If you'll be pleased I'll remove the language attribute and add type attribute to get valid xHtml tag but that is not what user asked.
Downvote's way harsh here. The user asked how to put a PHP variable in JavaScript, and this is an accurate response, modifying the user's code slightly to accommodate his request. It's a learning opportunity, I guess, but there are worse things than just answering the question itself. I'd recommend fixing the script tag to type="text/javascript", but this does answer the user's question.

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.