0

When i use the php and js separately it is working correctly

<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>

But when I use the php inside the js function it is not working correctly

<script>    var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>

How can make work my second code?

4
  • var test=<?php echo 'Master';?> ; try it this way Commented Apr 3, 2016 at 8:25
  • These lines are identical, aren't they? Commented Apr 3, 2016 at 8:25
  • Nah.. In the second code the php is inside the js function Commented Apr 3, 2016 at 8:27
  • I fixed the formatting - some of the content was hidden by using quote rather than code block. Commented Apr 3, 2016 at 8:28

2 Answers 2

2

The second code produces wrong HTML/JS:

<script>    var test = 'asdasdasd'; <script>document.write(test)</script> </script>

- see for yourself in the generated page.

Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.

I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.

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

1 Comment

<script> var test = 'asdasdasd'; <?php $id = "'+test+'" ; echo $id; //here i want to run sql code ?> </script>
1

You can insert any value from php to absolutely everywhere in your file. Literally everywhere. While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.

Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:

<script>
<script>
...
</script>

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.