Send Php Variable To Javascript

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Send Php Variable To Javascript

Supa Comix
Right i had a look across the internet as well as a search on here but you cannot search for anything less than 3 characters. But this is a really quick question.

Would the following code allow me to send a php variable to a javascript?

CODE
<?php
$color = "green";
?>
<script language="JavaScript">
BackColor= "<?php echo($color);?>";

</script>

Reply

ethergeek
I don't think this is possible...php and javascript are handled by two different preprocessors, one of them is php, the other is handled by the servlet engine. There may be a way to do it specifically using Tomcat and php installed into Tomcat, try the documentation...but as far as doing it with apache and the tomcat redirector...you may be out of luck.

Reply

vujsa
In short, yes!

there is no reason for this not to work. In fact, you could have PHP dynamically generate an entire JavaScript on the fly if you wanted. This is because PHP is parsed on the server so the browser only sees the output and JavaScript is parsed in the browser so what ever the browser sees, JavaScript can do...

Here is what your PHP code would output:
CODE
<script language="JavaScript">
BackColor= "green";

</script>


Now if you wanted JavaScript to send variables back to PHP, there is some issues but depending on what you want, this could be done using either the POST or GET method of variable passing but usually requires a new page to be loaded. It is possible with AJAX to pass variable to and from the server without the need for a new page load but that is another discussion.

vujsa

Reply

Supa Comix
Okay groovy thanks people. Thats exactly what i wanted.

Reply

TavoxPeru
QUOTE(Supa Comix @ Oct 23 2007, 09:01 AM) *
Right i had a look across the internet as well as a search on here but you cannot search for anything less than 3 characters. But this is a really quick question.

Would the following code allow me to send a php variable to a javascript?

CODE
<?php
$color = "green";
?>
&lt;script language="JavaScript">
BackColor= "<?php echo($color);?>";

</script>

Yes, it works fine. I do this kind of things every time.

Best regards,

Reply

Aldo
QUOTE(TavoxPeru @ Oct 24 2007, 08:04 AM) *
Yes, it works fine. I do this kind of things every time.

Best regards,



Heres what i always do:
CODE
<?php
$var="stored_variable";
?>
<script type="Javascript">
<!--
var string="<?php echo $var;?>";
//-->
</script>

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. reading php variable by javascript - 4.74 hr back. (1)
  2. hoe to generate an alert in javascript - 216.36 hr back. (1)
Similar Topics

Keywords : send, php, variable, javascript

  1. Web Editor
    great application for your html and javascript development (0)
  2. Php Location Header No Send Session Id ?
    (0)
    Hi, All I got more question on header function. re-read the php documentation and notice an term
    that said as QUOTE Note: Session ID is not passed with Location header even if
    session.use_trans_sid is enabled. It must by passed manually using SID constant. What that
    means ? I have used two seperate script : src.php and target.php something like this: > CODE
    session_start(); header('Location: /target.php'); > CODE
    print_r($_REQUEST); But it does the session id sent back from server to client, th....
  3. Conditional Statements Of Javascript
    Tutorial for beginers (1)
    In this tutorial we will discuss about conditional statements of JavaScript. JavaScript has 4 types
    of conditional statements such as if statement, if...else statement, if...else if....else statement
    and switch statement. All this statements are used in different condition. Now we will try to
    discuss this 4 types of statements with some examples. When we need to execute some code only in a
    specified condition, then we will use if statement. The syntax of if statement is if (condition) {
    code to be executed if condition is true } One important thing is that this if will....
  4. Simple Java Question
    One Variable; Multiple Classes (3)
    I’m probably the only person that is able to do so much with Java3D and yet still can't
    even get some of the Java basics down. Anyway is it (and if so how is it) possible to make a
    variable editable and readable in multiple class files? It seems like this should be an easy
    question to answer, but here is a little code to help you understand what I am talking about. CODE
    public class test extends Applet{  public int Col = 0;  [some long code basically all it
    does is call for a collision behavior]  [(TestB multiple times every 100 millisecon....
  5. Javascript Operators
    (0)
    In this part of the tutorial, we will discuss about JavaScript Arithmetic Operators, JavaScript
    Assignment Operators, Comparison Operators, Logical operators and conditional operator. +, -, *, /
    etc are the JavaScript Arithmetic Operators. =, +=, -= etc are the JavaScript Assignment Operators.
    ==, != etc are the Comparison Operators and “&&”, “||” etc are the
    Logical operators. Arithmetic operators are used to perform arithmetic between variables and/or
    values and Assignment operators are used to assign values to JavaScript variables. Comparis....
  6. Friends Can't Start The Exe Files I Send :(
    (3)
    I am using visual c++ 2008. and when I send the .exe file that I created to my friend, (from the
    debug folder) he gets an error message when trying to execute it /sad.gif"
    style="vertical-align:middle" emoid=":(" border="0" alt="sad.gif" /> any ideas? when my friend
    tried, he said that he got the following error message: cannot execute .... And lots of PC info.
    When I create these files, I choose win32 console application (something that looks like the cmd
    when executed) please.. I really need to get this thing working Thanks //Feelay....
  7. Email Problems
    Unable to Send or Receive (5)
    Ever since the server move (migration, relocation, whatever you want to call it) I've had a
    problem with both sending and receiving emails. I use Microsoft Outlook Express as my email
    application, and originally hadn't changed any of my email settings (figuring that there
    wouldn't be any difference between the two). I'm unsure of exactly when the problem started,
    as I get no errors or messages indicating that it was failing to receive messages (getting the
    normal "connecting, authorizing, checking mail" series of stages in the process). It was only when I....
  8. Javascript Tutorial For Beginner
    Using Javascript in HTML page (0)
    In this tutorial I will show you how you can put JavaScript in a HTML page. It is very easy to add
    JavaScript in a HTML page. We will use <script> tag for this purpose. Inside the <script>
    tag, we will use "type=" attribute and will define the scripting language. We will define the script
    language as “text/javascript”. After define the script language we will add our
    JavaScript codes and at last we will close the tag using . So the complete code will be- CODE
    <html> <body> <script type="text/javascript"> (Pla....
  9. Include Function For Javascript
    (7)
    I've been working on an include function for javascript. It works just fine in Firefox and IE,
    but for some reason, it doesn't result in the loading of the scripts for Safari. The code is as
    follows: CODE function include(url) {   // Include Guard   var scripts =
    document.getElementsByTagName("script");   for (var index = 0; index <
    scripts.length; ++index) {     if (scripts.src == url) {       return;     }   }      //
    Inclusion   var head = document.getElementsByTagName("head").item(0);   ....
  10. Extplorer
    A PHP -and JavaScript- based File Manager (7)
    Browsing the ExtJS examples website i found this excellent web-based file manager called
    eXtplorer . eXtplorer allows you to browse your webserver folders with an intuitive Layout which
    makes working with files very easy, and thanks to the great ExtJS Javascript Library you can drag
    & drop folders and files, filter directories and sort the file list using various criteria. You can
    use eXtplorer to for example: browse directories & files on the server. edit, copy, move, delete
    files. search, upload and download files. create and extract archives. create new fil....
  11. Enumeration Enum = .....
    Variable identifier a keyword? (4)
    Oseveral occasions when I was perusing example source code from various locations, I see something
    like this: Enumeration enum = headers.keys(); while (enum.hasMoreElements()) { String name =
    (String) enum.nextElement(); Forgive me here, but to me it looks like they're declaring a
    variable's identifier to be a java keyword...? I've seen this more than once, but ran across
    it just now in the com.oreilly.servlet package source. Eclipse always gives me errors when I load
    example code containing something like this, so I'm sure it's incorrect. What&#....
  12. I Need Help With Javascript.
    help plz (7)
    Well, I want to learn to code in JavaScript, but when I try to view the page in Firefox, it
    dosen't work. Can anyone tell me why its not working? Do i need to use a different browser?....
  13. How Do I Send SMS To Mobiles Through My Site
    (9)
    I want to send sms to my users through my website ,is there any way to do it with php or any other
    sofware, what i want is to help my users get their private message alerts on their cell phones, is
    there anyway to do it without tieing up with the smm providers on the web ,like they tell to pay you
    few cents per sms ,is there anyway i can do it free, like cani become a provider just like them ,or
    make a way like, if a user agrees to pay for alerts ,than whenever an alert goes the amount gets
    deducted from the user's cell phone , i am a newbie ,so dont know much hoe th....
  14. OSCommerce Errors
    Passed variable is not an array or object (5)
    I don't know where this topic is appropriate in so please move it if it's innapropriate for
    this place as this involves PHP and OSCommerce Hi, I got an error stating the following: Warning:
    reset(): Passed variable is not an array or object in
    /home/content/j/w/s/jws8118/html/catalog/admin/includes/classes/object_info.php on line 17 Warning:
    Variable passed to each() is not an array or object in
    /home/content/j/w/s/jws8118/html/catalog/admin/includes/classes/object_info.php on line 18
    Here's the source code: CODE <?php /*   $Id: object....
  15. Php Send Mail Through Smtp
    (8)
    Can anyone here tell me how to send mail through SMTP server with php /mellow.gif"
    style="vertical-align:middle" emoid=":mellow:" border="0" alt="mellow.gif" /> I have search in many
    source code on web and cant find anything /sad.gif" style="vertical-align:middle" emoid=":("
    border="0" alt="sad.gif" />....
  16. Want To Create A Community Website
    Hi everyone,
    i am planning to setup a community website with boon
    (4)
    Hi everyone, i am planning to setup a community website with boonex's dolphin package, please
    send ur thoughts and ideas.......
  17. How To Reset The Server Variable Php_auth_user
    (9)
    Hi, i'm developing a web application which obviously requires a log in/log out script that i
    just implementing but i dont know why the log out script dont work fine. The problem is related
    with the server variable $_SERVER which remains set even when in the log out script i unset it
    with the unset() function. Does someone knows how can i reset or clear the server variable
    $_SERVER ??? Best regards, ....
  18. JavaScript Frames & Querystring
    (4)
    Hi, I actually want to create a HTML page which has the capablity of reading a QueryString
    (x.html?querystring=test) and then using a hidden frame pass on the data to an ASP Script on another
    server. For obtaining the QueryString i use the following:- CODE <script> function
    getQueryVariable(variable) {   var query = window.location.search.substring(1);
      var vars = query.split("&");   for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");     if (pair[0] == variabl....
  19. Create And Import JavaScript Modules For A Large Script
    (2)
    I have one main script which defines an object. I then have several other script files that define
    functions for that object. I have tried using the following setup to import the functions: HTML
    code snippet: <script type="text/javascript" src="/foo/mainbar.js"> <script
    type="text/javascript" src="/foo/modulebar.js"> Javascript code snippet: import
    Object.functionName; This works fine for Mozilla Firefox, but causes an error in Internet Explorer
    which causes the object to be undefined. I would like to know how better to import the functions
    into the m....
  20. Ever Needs To Find Out A Table Height Or With With JavaScript
    Well stop looking, here is the answer (CSS and JS) (2)
    Welcome everybody to this litle tutorial. by v.DragonEyE.n09 Introduction: Using
    javascript you can find the height and width of a table, cell, div, image, etc.. the more simple way
    is to ask for this... QUOTE id= "myElement" border= "0" cellpadding=
    "0" cellspacing= "0" style= " height : 300px ; width
    : 450px ; " > some
    fake text and images for the example some fak....
  21. Best Way To Learn Javascript
    I would very much like to know. (9)
    Well, I know HTML, I know CSS, so the next step is learning Javascript. I have heard that it is sort
    of an easier way to do things than PHP, though I would very much like to learn PHP as well. So, I
    thought that if I learnt Javascript, I might find it easier to learn PHP when I get to that point.
    The problem though is actually finding out the best way to learn Javascript. I have been to
    websites and things, but every time I read through the tutorials and articles and things, I
    didn't learn anything. It when in one ear and out the other, you could say. So, that is wh....
  22. Hack Websites
    using a simple javascript (8)
    Sometimes, I come across websites that are very annoying, like webpages which play embedded music
    files or excessive animations. Seems that people who encouter such websites like me have a solution
    is you use Firefox. Just use an extension and you can hack and alter these websites and
    webpages! According to Wired (http://www.wired.com/wired/archive/13.09/start.html?pg=7):
    QUOTE Remix culture has hit the browser. Just as you can pimp your Scion with snap-on parts, you
    can modify Web sites to suit your tastes - whether the authors like it or not. The enabling tec....
  23. Creating Tooltips
    DHTML, HTML, CSS, Javascript... (7)
    Here is a little tutorial to make those small yellow-background boxes (tootltips) that pop-up for
    some links that describe them when you hover the mouse over them, it uses DHTML, CSS, HTML, and
    Javascript, so it gives us a much more wide range than my previous tutorials that just where on
    HTML. OK, so here we go... Put this right under CODE <div
    id="dhtmltooltip"></div> <script type="text/javascript"> var
    offsetxpoint=-60 //Customize x offset of tooltip var offsetypoint=20 //Customize y offset of tooltip
    var ie=document.all....
  24. Random Images On Eacht Pageload And Refresh.
    best in plain htlm or javascript (6)
    Im looking for somethin that puts a diffrent image for each pageload/refresh. And it should be
    something that dosent need any super server technolgys like php and mysql and all that stuff.
    something that is enough just on the page and finished.....
  25. How To Send Email Using VB (using SMTP) ?
    best way to send email in vb (8)
    Hi everybody, I want to send an email to multi users through my VB application. I don't know if
    I can use a VB class for this case or I should download a smtp mail component. I would be thankful
    if you can suggest me the best solution. Please give me a code example for former solution or
    introduce me an effective free, permanent component.....
  26. Code To Send An Email From A Form
    (7)
    HTML form action =" http://www.bool.co.il/cgi-bin/bu.cgi?page=send2friend&id=8006 "
    method=" post "> font face =" Arial "> input type =" text " name=" sender "> /font > the
    sender: br > input type =" text " name=" sender_email "> sender's mail: br > font face
    =" Arial (hebrew) "> input type =" text " name=" receiver "> /font > to: br > input
    type =" text " name=" receiver_email "> email Of receives the announcement: br > font face ="
    Arial (hebrew) "> input type =" text " name=" subject "> /font > the subject: br > i....
  27. Javascript: Disable Mouse Right-click In Browser
    (16)
    just copy and paste this on to your html site in the head tag CODE <script
    language="JavaScript"> <!-- /* */ var message="Hey YOU!\nStop
    Stealing my pictures or\nFACE THE PUNISHMENT!" function click(e) { if
    (document.all) { if (event.button == 2) { alert(message); return false; } }
    if (document.layers) { if (e.which == 3) { alert(message); return false; } }
    } if (document.layers) { document.captureEvents(Event.MOUSEDOWN); } document.onmouse....
  28. javascript vs java
    (12)
    in my opinion javascript is better then java becasue java requires more programing experecense
    then javascript and i think to that javascript is more reliable then java in some ways....
  29. How To Create A Popup Window With Javascript?
    (19)
    Please help me! I want to in popup massage suggest to visitors of my site to make my site their
    home page . Please note that I have some script code that visitors most click on a text to make my
    site their home page . Every one that can help me please send an email to soleimanian@noavar.com....
  30. Getting Screen Resolution using Javascript.
    (8)
    Is it possible to get the screen resolution of the users moniter using only javascript. No other
    external stuff, just pure javascript. I guess most of the people using JAVA for this. I also got
    some scripts regarding this which used SCREEN.WIDTH function or something, but they did not seem to
    work out. Any possible solutions. BTW, is it possible to get the resolution using PHP ( I guess it
    is not ) .......

    1. Looking for send, php, variable, javascript

Searching Video's for send, php, variable, javascript
Similar
Web Editor -
great
application
for your
html and
javascript
development
Php Location
Header No
Send Session
Id ?
Conditional
Statements
Of
Javascript -
Tutorial for
beginers
Simple Java
Question -
One
Variable;
Multiple
Classes
Javascript
Operators
Friends
Can't
Start The
Exe Files I
Send :(
Email
Problems -
Unable to
Send or
Receive
Javascript
Tutorial For
Beginner -
Using
Javascript
in HTML page
Include
Function For
Javascript
Extplorer -
A PHP -and
JavaScript-
based File
Manager
Enumeration
Enum = .....
- Variable
identifier a
keyword?
I Need Help
With
Javascript.
- help plz
How Do I
Send SMS To
Mobiles
Through My
Site
OSCommerce
Errors -
Passed
variable is
not an array
or object
Php Send
Mail Through
Smtp
Want To
Create A
Community
Website - Hi
everyone, />i am
planning to
setup a
community
website with
boon
How To Reset
The Server
Variable
Php_auth_use
r
JavaScript
Frames &
Querystring
Create And
Import
JavaScript
Modules For
A Large
Script
Ever Needs
To Find Out
A Table
Height Or
With With
JavaScript -
Well stop
looking,
here is the
answer (CSS
and JS)
Best Way To
Learn
Javascript -
I would very
much like to
know.
Hack
Websites -
using a
simple
javascript
Creating
Tooltips -
DHTML, HTML,
CSS,
Javascript..
.
Random
Images On
Eacht
Pageload And
Refresh. -
best in
plain htlm
or
javascript
How To Send
Email Using
VB (using
SMTP) ? -
best way to
send email
in vb
Code To Send
An Email
From A Form
Javascript:
Disable
Mouse
Right-click
In Browser
javascript
vs java
How To
Create A
Popup Window
With
Javascript?
Getting
Screen
Resolution
using
Javascript.
advertisement




Send Php Variable To Javascript



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE