admin管理员组文章数量:1410674
var page_my_code_is_on = (document.referrer);
document.write("<?php echo '" + page_my_code_is_on + "'; ?>");
how can I write the above php as a var within the js, as opposed to echo
?
I read the following but it did not work for me
.php
var page_my_code_is_on = (document.referrer);
document.write("<?php echo '" + page_my_code_is_on + "'; ?>");
how can I write the above php as a var within the js, as opposed to echo
?
I read the following but it did not work for me
http://www.webcheatsheet./PHP/passing_javascript_variables_php.php
Share Improve this question edited Nov 25, 2011 at 16:10 Clive 37k8 gold badges89 silver badges113 bronze badges asked Nov 25, 2011 at 16:09 daily-learnerdaily-learner 6271 gold badge9 silver badges18 bronze badges 7- Based on the wording of your question, and the oddity of the concepts shown in your code, I can't tell what you're asking. Can you clarify what you're attempting to do? – JAAulde Commented Nov 25, 2011 at 16:12
-
1
PHP is a server-side language and unless you are sending something to a php (via AJAX for example), it can't
echo
something after rendering, that's not how PHP works. – jackJoe Commented Nov 25, 2011 at 16:12 - 1 JavaScript works on the client side, PHP on the server side. If you add PHP-code on the client-side, it can't be executed without a roundtrip to the server (e.g. with AJAX). What exectly are you trying to achieve? – Quasdunk Commented Nov 25, 2011 at 16:14
- Why not try something like node.js? Thats also serverside javascript. – user753676 Commented Nov 25, 2011 at 16:15
- 1 looking at the link you provided, the way that works is to redirect the page to itself with the queries the javascript sent to the PHP, and then the PHP will process them and render the page. In that context it makes sence, but understand that it only works because the page is redirected and rendered again, otherwise it wouldn't do anything. – jackJoe Commented Nov 25, 2011 at 16:15
4 Answers
Reset to default 9It doesn't work this way. You can deliver PHP variables to JavaScript by echoing a JavaScript definition that will then be interpreted by the browser. If you want to bring a JavaScript variable to PHP, you will have to do a POST or GET request. AJAX would be possible as well.
Remember that JavaScript is executed in the user's browser and PHP is executed on your server. There is a huge difference.
You can't do it this way. This is how it goes:
Page is loaded on server, PHP is executed on the server.
Page is sent to client (user)
Javascript is executed at the user puter.
What you want is:
Page is loaded, javascript executed.
PHP is executed.
Page is sent to user.
What you want is:
document.write("<?php echo $_SERVER["HTTP_REFERER"]; ?>");
In your example, you're writing out a string to screen. I assume you see the following in your browser:
<?php echo 'XXX'; ?>
where X is the contents of page_my_code_is_on?
Are you trying to access the document.referrer in PHP, if so, why not use PHP's $_SERVER["HTTP_REFERER"] variable?
you'll have to re-pass page_my_code_is_on
to a php file..
for example with:
window.location.href = "http://www.example./index.php?page_my_code_is_on=" + page_my_code_is_on;
and in the index.php
you can access to it with $_GET["page_my_code_is_on"]
index.php:
<?php echo $_GET["page_my_code_is_on"]; ?>
(you can't write php in javascript and execute it)
本文标签: generate php in javascriptStack Overflow
版权声明:本文标题:generate php in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744996066a2636680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论