Reputation: 4518
simple jquery document ready and then I need some value from php to be alerted for example remote address.
$(document).ready(function() {
string1="<?= $_SERVER['REMOTE_ADDR'] ?>";
alert(string1);
When I write like this result is
<?= $_SERVER['REMOTE_ADDR'] ?>
What's wrong ? no way to pass some php inside js file ?
Upvotes: 0
Views: 106
Reputation: 15872
Make sure that your web server allows tags, maybe try . Secondly rather than trying to write to a .js file. You should use a .php file and write the JavaScript inside the HTML within tags.
Upvotes: 0
Reputation: 761
PHP code cannot be executed in files with extensions other than .php/.php5
(you might have used .js or .html)
Try this
<?php echo "<script type=''text/javascript>"; ?>
$(document).ready(function() {
string1="<?php echo $_SERVER['REMOTE_ADDR']; ?>";
alert(string1);
});
<?php echo "</script>"; ?>
Save the file as .php
Upvotes: 0
Reputation: 944530
no way to pass some php inside js file ?
The file has to be run through the PHP pre-processor. This is usually done by giving the file a .php
file extension (although you can configure webservers so that isn't needed).
If you are serving up something that isn't HTML, then make sure you specify the content type.
header("Content-Type: application/javascript");
Upvotes: 4
Reputation: 4003
From what I know you can't, because JS is client based and PHP is server based.
Upvotes: 0