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
 |  Show 2 more ments

4 Answers 4

Reset to default 9

It 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:

  1. Page is loaded on server, PHP is executed on the server.

  2. Page is sent to client (user)

  3. Javascript is executed at the user puter.

What you want is:

  1. Page is loaded, javascript executed.

  2. PHP is executed.

  3. 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