Jump to content

document.write as a variable


dreamwest

Recommended Posts

How can i store document.write as a variable

 

This outputs the country code "AU"

<script language="JavaScript">document.write(geoip_country_code());</script>

 

Now i need to use it in a php script

 

<?
$code =   ;//document.write output here

?>

Link to comment
Share on other sites

so, since JavaScript is run client side, and PHP is server side, you can't directly use it. You need to send it back to the server, and for that you have a couple of options.

 

1) Do an AJAX call. If you are going to do this, I would suggest checking out jQuery...it will make your life way easier. Basically, you can submit data in the background.

 

2) If this is the only value you care about though, it's probably easier to just pass it in a resource call. The resource could be a CSS file, JS file, or an image. For example:

<script language="JavaScript">
  var img = new Image();
  img.src = 'geo_track.php?geo='+geoip_country_code();
</script>

The browser will request an image at that url, where you can have a PHP script to process the info, then sends back a 1 pixel blank image or just nothing at all

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.