john
john

Reputation: 1330

get contents of javascript value into a php variable?

i have a client who is required to use specific tracking that is only made available through javascript. he wants to be able to capture the exact same information being printed on the client side.

how can i add this into the existing php script to add the data into the database? my only idea was to echo the javascript directly inside the main script, but i can't seem to figure out how to get the values out of the php echo.

any ideas?

Upvotes: 3

Views: 1624

Answers (3)

user743234
user743234

Reputation:

It is not possible, because client-side script can NOT interact directly with server-side script, therefore it requires you to have a "transporter" in the between, which is AJAX. Have a look at jQuery AJAX, it's tiny, sweet and powerful.

But assume there is a small chunk of information, and it's not very confidential, I can suggest you to do this way, using cookies. People don't usually take advantage of cookies.

<script type="text/javascript">
document.cookie = 'info='+information; expires=Mon, 19 Jul 201110:00:00 UTC; path=/';
</script>

In PHP file

<?php
if (isset($_COOKIES['info'])) {
   $information= intval($_COOKIES['info']);
}
?>

Once you finish getting the information, you can delete that cookies.

Upvotes: 0

Drazisil
Drazisil

Reputation: 3343

You can send it to the server using a dummy image.

document.write('<img src="file.php?foo=value1?bar=value2&...">');

contents of file.php

<?PHP 
Header("Content-type: image/png"); 

do something with $_GET['foo']

?> 

It's not the best way, but it's fast and it will work.

Upvotes: 1

maaudet
maaudet

Reputation: 2358

It's not really possible, you'd need to do an AJAX call to get something similar.

I suggest implementing the code in PHP directly rather then reliying on Javascript for data.

Upvotes: 3

Related Questions