1

I looked around but couldn't find an answer to my current issue.

I want to echo a PHP variable into my external .js file. I then use the PHP variable in a javascript variable and from here use ajax to send the variable/s to my database. I believe I am using incorrect syntax for the php echo. My javascript

content = '<div class="post">' + '<img src="<? echo $pic ?>" />' + '</div>';

Is this possible to echo the PHP variable to the external .js file? Am I using the right syntax in the javascript variable?

1
  • If the .js file is being served statically, then no, PHP cannot modify it. Commented Jul 7, 2013 at 23:24

2 Answers 2

2

Your server will not parse the .js file as PHP. You could name your js file as a .php file, and then use header("Content-type: application/javascript").

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

Comments

0

Normally, your httpd is only set to parse files named 'foo.php' for PHP code; name the file foo.php instead of foo.js and manually set the content type:

<?php header('Content-type: application/javascript');?>
content = '<div class="post">' + '<img src="<? echo $pic ?>" />' + '</div>';

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.