1

Ok, so I have a few things here:

Javascript:

desc = "line 1 \n line 2"

jQuery:

$("#msg").text(desc);

PHP:

const NUM = 555;

What I want, is to change the text of the <p> with the id of msg, so that it would contain a piece of text with a number of lines, and in one of them the number from the PHP constant.

Like so:

Line 1
Line 2 555, Line 2 continued
Line 3

My problem is how do I mix them all? I tried the following:

var desc = "line 1 \n line2" + <?php echo NUM ?> +"\n line 3"; and that doesn't work.

4
  • "and that doesn't work." What doesn't work? Do you get an error, or is it in wrong order? Commented May 8, 2013 at 16:32
  • Try <?php echo constant("NUM")?> instead? Commented May 8, 2013 at 16:33
  • What's the expected output? I'm not understanding line 1 line 2 line 3. Commented May 8, 2013 at 16:34
  • The expected input is just 3 lines of text, while in one of them I have the number which is stored in the PHP constant. About the error - I get 'Unexpected token ILLEGAL'. Commented May 8, 2013 at 16:39

1 Answer 1

2

There are several issues with your code:

  • PHP constants should be defined using define("CONSTANT_NAME", "VALUE"); syntax;
  • \n has no effect inside HTML tag (if you dont apply white-space: pre; or pre-wrap);
  • <?php echo NUM; ?> should be wrapped with " or should be inside JavaScript string;
  • $("#msg").text(desc) will remove all tags from desc, thus you need to use .html(desc) instead.

What you need is something like this:

PHP

define("NUM", 555);

JavaScript

var desc = "line 1<br/>line2 <?php echo NUM; ?><br/>line 3";
$("#msg").html(desc);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 because this looks right, but the question is not very clear at all ... so i can't tell.
@frrlod do you understand why it worked? that's more important than just having something that works.
@frrlod I updated the answer with description what was wrong with your code.

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.