Jump to content

JS and php


Derleek

Recommended Posts

forgive me for this silly question, but i'm just looking to use some basic JS for a script i'm developing.

 

How are variable's handled between JS and PHP?

 

Lets say i have a variable $msg defined in a php sript.  If i wanted to use that in a JS function... do I have to do anything special to pass a variable from language to language?

Link to comment
https://forums.phpfreaks.com/topic/116326-js-and-php/
Share on other sites

How are variable's handled between JS and PHP?

 

well shortly its not but there is a way to pass a variable from php to javascript.

 

1. php is serverside which means it(usually) runs on remote computer with apache or IIS. the server handles the php and throws out html only. And the users viewing a page never gets to see a bit of php. which is a good thing think of how easily it would be to hack into a system if you could see the php source in a browser.

 

2. javascript is (in most casses) clientside which means it runs in the browser ie firefox etc. in other words it runs on the computer of the person viewing a webpage.

 

but anyway back to passing variables from php to javascript.

 

what you basicly want is letting php write javascript

 

on your server

<script>
var myJavascriptVar='<?php echo("my php string");?>';
//alert the value
alert(myJavascriptVar);
</script>

 

in your browser select view source now it will look like

<script>
var myJavascriptVar='my php string';
//alert the value
alert(myJavascriptVar);
</script>

 

and this is exactly what is being done in your browser

 

Link to comment
https://forums.phpfreaks.com/topic/116326-js-and-php/#findComment-598399
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.