0

I use in my HTML Markups always Underlines, which are programmed with PHP and will replaced by strings. For example:

<title>__TITLE__</title>

So in this way I have just one template of HTML but my pages although have their own title names.

But in one case I have a problem. I also use JQuery and I have this case:

$("div").append("<p>__NAME__</p>");

In this case there is the text "__NAME__" on my Homepage, not e.g. "John".

Can someone help?

4
  • You append the text, after the PHP script ran. Commented Aug 20, 2020 at 7:22
  • 1
    You need to assign the PHP variables to JS variables, e.g. var __NAME__ = <?php echo json_encode($name); ?>;. Then you can use the variable __NAME__ in JavaScript. Commented Aug 20, 2020 at 7:23
  • Is this jQuery code contained in yout HTML template, or is it in an external script? If it is the former - then where is your actual PHP code that does the replacing, show it. Commented Aug 20, 2020 at 7:26
  • I think you're mixing up javascript from php. Can you show the full source code here? Commented Aug 20, 2020 at 7:33

1 Answer 1

1

You could add a placeholder div for it in your HTML:

<div id="template1" style="display:none">__NAME__</div>

Then in your code, pick up the contents of that element (which will have been altered by PHP):

$("div").append($("<p>").html($("#template1").html()));
Sign up to request clarification or add additional context in comments.

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.