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