LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-10-2006, 12:09 PM   #1
Kanaflloric
Member
 
Registered: Jun 2004
Location: Canada
Distribution: Slackware 10.0
Posts: 80

Rep: Reputation: 15
communicate a php variable to a javascript function


Hi

Suppose I have a php function that returns a string (for exemple a name).

<?php
function findName()
{
...
return something
}
?>

I can do some stuff like

<?php
echo findName();
?>

But HOW could I use it inside a javascript function ?

<script language="JavaScript">
document.NewVar = ...ooops ?
</script>

I don't think php can acces the document object ?
 
Old 08-10-2006, 02:34 PM   #2
silent_cutthroat
LQ Newbie
 
Registered: Nov 2005
Distribution: Arch
Posts: 27

Rep: Reputation: 15
You can't, at least directly. But you can use php to dynamicaly generate the javascript code.

Code:
<script language="JavaScript">
document.NewVar = <?php echo findName() ?>;
</script>
The php code is interpreted on the server and has nothing to do with the html rendering or javascript execution. It's only used to generate the page After the page is generated the browser interprets the javascript in it.
 
Old 08-10-2006, 03:31 PM   #3
Kanaflloric
Member
 
Registered: Jun 2004
Location: Canada
Distribution: Slackware 10.0
Posts: 80

Original Poster
Rep: Reputation: 15
Thanks

I didn't know i could insert a <?php> tag inside a <script> tag...!
 
Old 08-10-2006, 07:41 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
Quote:
Originally Posted by Kanaflloric
Thanks

I didn't know i could insert a <?php> tag inside a <script> tag...!
You are suffering from a lack of comprehension about the difference between php and javascript.

PHP is a programming language that runs on the server. Its output is (commonly) a web page done in html and javascript.

Thus, when you do things in PHP you commonly are doing it in order to create the page that you present to the user. The page you create is created on the server and shipped to the client, which is the computer the user is at.

Since you are creating the page to ship, you can create anything you want. The HTML tags mean nothing to PHP; they are just text to be manipulated.

Any time you insert a <?php ?> tag, you are telling the server to execute the code found between the opening and closing tags. Anything that is outside the PHP tags is not executed, merely shipped to the output. So, for instance, if I had the HTML string that went like this:
[HTML]<p>Ship this string of text with this <a href="very cool link">cool link</a> to the client</p>[/HTML]

I could do it with a mixture of PHP and HTML like this, for instance:
[HTML]<?php
$myvar="ool link\">coo";
?>
<p>Ship this string of text with this <a href="very c<?php echo $myvar;?>l link</a> to the client</p>
[/HTML]
Which would give me the exact same display on the client's machine. Now, granting that this is a trivial example, it is indicative.

You can construct any kind of HTML or javascript using PHP on the server.
 
Old 08-11-2006, 12:58 PM   #5
Kanaflloric
Member
 
Registered: Jun 2004
Location: Canada
Distribution: Slackware 10.0
Posts: 80

Original Poster
Rep: Reputation: 15
Yes, I see now.

I had a "traditional-programming-point-of-view" on this subject, wich seems almost harmful in this context.

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php function->query(variable) what's that? osio Programming 3 06-12-2006 06:48 AM
pass javascript variable to php ALInux Programming 6 01-06-2006 06:20 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
PHP Variable = function? wh33t Programming 2 01-22-2005 06:28 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM


All times are GMT -5. The time now is 08:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration