Nov 23, 2009

Send Php Variable To Javascript

free web hosting
Open Discussion & 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>

Comment/Reply (w/o sign-up)

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.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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,

Comment/Reply (w/o sign-up)

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>

Comment/Reply (w/o sign-up)


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*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : send, php, variable, javascript

  1. Permanent Variable
    (7)
  2. 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....
  3. Unexpected Error
    Undefined variable??? (2)
    Is this script correct? index.php : CODE     if (isset($_COOKIE ))           $_GET =
    $_COOKIE ;     else         setcookie("disp_name", Anonymous, date()+99); ?>              
    Display Name - DZN                                                                   
                    Dislpay Name: name="name" />                                                
         proccess.php : CODE      Proccessing Request...           Please wait...
        setcookie("disp_name", $_GET , date()+99); ?> main.php : CODE     function customErr....
  4. Php Any Variable In String.
    (1)
    OK well I am making a new php program and I am trying to add bbcode to it. Anyway I was going to
    replace each thing by it self, but that could cause errors. Anyway is there a way to make a variable
    be anything? Here is an example: CODE This is the bbcode: Hey In my php: $bbcode = array(" ");
    $html = array( " "); $topic_content = str_replace($bbcode, $html, $posted_bbcode); So I want $a
    to be any variable but the same variable as used before. Do you get what I am saying? It is a little
    confusing but basicly what is the best way to make bbcode? It may not even inv....
  5. Quickly Create Form Variables
    simple form, variable creation, referer check, safe guard variables (5)
    The reason I wanted to share this is I've seen so many people do this with their forms when
    using PHP. CODE $username = $_POST ; $password = sha1($_POST ); $another_var = $_POST ; ...
    and so on, just imagine if you had a large number of form inputs, do you really want to create each
    and every variable name? Why people do this, is probably due to most of the examples I've seen
    on the web, that does not show an easier and much quicker way of doing it. Though my way might be
    much easier and quicker, it does introduce security concerns which I've tried....
  6. Variable From Line Further Then Current Line?
    (13)
    Hello, Is it in some way possible to load a variable that further then the current line in the
    script? I don't know if you know what i mean so I'll try to point it out in the script
    below. CODE 1 2      echo $var1; 3      # $var1 needs to be loaded from foo.php 4     # Lets
    say i want $var1 to be echoed in $var1 5       - - - -       bla bla bla 55      - - - -
    56      switch($_GET ) { 57          case "foo": 58              include("foo.php");
    59              #- - - - from foo.php - - - - 60                   $var1 = "what ever you want";
    61           ....
  7. Need Help With Javascript In Php Urgently.
    (4)
    If you look at: http://www.chessoscorner.com/tools/google_...p_generator.php You can see it
    produces an error like this: Parse error: syntax error, unexpected T_STRING in
    /home/chesso/public_html/tools/google_sitemap_generator.php on line 65 This particular section is
    not PHP code. The php TAG has been ended before my javascript code and reopened after my javascript
    code........ This page however works perfectly using WAMP5 locally on my pc (may have something to
    do with it using a newer version of PHP). Here is the javascript code in question: CODE   
    func....
  8. 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, ....
  9. Form Validation
    Javascript (10)
    Okay, I know how to use cgi or php to make sure a form is filled out the way I want, and if not,
    post up a page marking what needs to be fixed. I also know how to make a javascript to alert what
    needs to be fixed and not procede until they are fixed. What I want to do is use javascript to mark
    everything that needs to be fixed without using alerts, probably by changing the color. Any ideas
    as to how to do this?....
  10. Php Variable Concatenation
    Something New I learned Today! :P (7)
    I was coding one of my php page today, when I realized that I had to add multiple values to a
    variable (now that I think of it, a solution with arrays is possible too). But, the problem is that
    I have to add them in different parts of the code, so the new line that defines the variable will
    cover up the previous one. I played around with the code, and I finally got this solution:
    $message = "test1"; $message .= "test2"; $message .= "test3"; echo $message; By adding a dot in
    front of the = sign, I could concatenate the previous value with the new one and put them i....

    1. Looking for send, php, variable, javascript

See Also,

*SIMILAR VIDEOS*
Searching Video's for send, php, variable, javascript
advertisement



Send Php Variable To Javascript

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com