2
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.com/PHP/passing_javascript_variables_php.php

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? 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. 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? Commented Nov 25, 2011 at 16:14
  • Why not try something like node.js? Thats also serverside javascript. 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. Commented Nov 25, 2011 at 16:15

4 Answers 4

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.

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

4 Comments

i need to get the js var (page_my_code_is_on) to php var. how via post or get? var page_my_code_is_on = (document.referrer); document.write("<?php echo '" + page_my_code_is_on + "'; ?>");
You put it into a (hidden) field in a form and submit the form. Or you call another page and append ?location= and your variable value. In both cases you would leave the active page however. Only AJAX could prevent this.
I would recommend jQuery for this task. Make sure however, that you really need to do this. You can pick up the referer just as well via PHP using $_SERVER['HTTP_REFERER'] and then no JavaScript is necessary at all.
I'm using document.referrer because I want users to put this iframe on their site and it reports the links of the site back to me . the difficulty is getting the js var onto a php var <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
1

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

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"]; ?>");

3 Comments

how do I set that to a php var in js ? s'thing like.....document.write("<?php $var= $_SERVER["HTTP_REFERER"]; ?>");
I'm using document.referrer because I want users to put this iframe on their site and it reports the links of the site back to me . the difficulty is getting the js var onto a php var <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
I think I got it can you try this on a page and see if it writes your page's link to the page <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
1

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?

5 Comments

I'm using document.referrer because I want users to put this iframe on their site and it reports the links of the site back to me . the difficulty is getting the js var onto a php var <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
Was about to submit the same thing before I looked up and saw this solution. $_SERVER globals are probably what you're looking for. That way you can do everything in PHP.
@RafaelDaCosta I'm not sure that is possible due to security issues that would cause.
I think I got it can you try this on a page and see if it writes your page's link to the page <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
So the iframe needs to have the ability to see the document requesting it? Why not use PHP to append a query string to the end of the page name to the URL and access that from the page? So you would have something like: <iframe src='collectivedashboard.com/facebook/test_url_ping.php?referer=<?php echo url_encode($_SERVER['REQUEST_URI']); ?>'; frameborder='0' height='500' width='250' scrolling='no'></iframe> Then the file test_url_ping.php should be able tp read $_GET['request']
0

you'll have to re-pass page_my_code_is_on to a php file..

for example with: window.location.href = "http://www.example.com/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)

2 Comments

don't want to leave the page though... I'm using document.referrer because I want users to put this iframe on their site and it reports the links of the site back to me . the difficulty is getting the js var onto a php var <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>
I think I got it can you try this on a page and see if it writes your page's link to the page? <iframe src='collectivedashboard.com/facebook/test_url_ping.php' frameborder='0' height='500' width='250' scrolling='no'></iframe>

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.