Click to See Complete Forum and Search --> : Interfacing PHP and JavaScript ???


chestertb
10-10-2003, 07:52 AM
Hi All,

I am so unfamiliar with PHP that even the "PHP in 24 Hours" book I bought is a litle overwhelming. No need to "sell" me on the benefits... sooner or later I will get my head around the language but in the meantime, I was hoping someone can help with a tiny problem.

I'd like to get the visitor's IP address (which I'm sure PHP can do), and somehow get that value to a JavaScript variable.

The ideal would be to be able to call a PHP function from within a JavaScript function and have that function simply return the IP address... but I am totally open to suggestions.

Help really appreciated.

Thanks
IB

pyro
10-10-2003, 08:26 AM
Importing PHP values into JavaScript is easy (the other way is a bit harder). Since PHP is executed server-side, it's easy to output to the page. Try something like this (remember the PHP files need to have a .php extention):


<script type="text/javascript">
var ip = "<?PHP echo $_SERVER['REMOTE_ADDR']; ?>";
alert (ip);
</script>

chestertb
10-10-2003, 08:31 AM
much appreciated... I'll give it a shot.