FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


JavaScript in PHP





aruncode47
I want to combine Javascript with PHP..
Some simple examples for beginner..
Any suggestions???
badai
you got to understand the concept:

php will be process at server and your browser will only see the output of it, if any.

javascript will be ignore by server and will be dump to your browser, and your browser will display it.

php will always be process/run first by server, and whatever the output along with anything outside the php tag will be dump to your browser.

the easy way to visualise your code is: replace everything in php tag to whatever the result of the php code is doing to plain text, that is what your browser will see.

you can always mix php and javascript.

you can even "write" javascript using php like:
echo "<script>document.write('hello');</script>";

but you cannot "write" php using javascript like this:
document.write("<?php echo 'hello'; ?>");

of course, the above example actually work because your browser will see:
document.write("hello");

that is because the statement
<?php echo 'hello'; ?>

has been processed at server and the output is
hello
jmraker
Javascript isn't limited to browsers. Google's has separated the v8 javascript engine from the browser ( https://developers.google.com/v8/embed ) and can run as a simple server ( http://www.nodejs.org http://socket.io/ )

In PHP most of the time you'd just include a javascript file by outputting the tag
Code:
<script type="text/javascript" src="TheJavascriptFile.js"></script>
for every javascript file you need to include
and you can include inline javascript like
Code:
<script type="text/javascript">var name1="John Doe";</script>


While outputting the script tag you can output the value of variables or the return values of many of the built in php functions
Code:
<?php ...
echo '<script type="text/javascript">var object1=' . json_encode($object1) . ';</script>
...
?>

which will output the variable $object1 as an JavaScript Object Notation encoded string
nepalstar
badai wrote:
you got to understand the concept:

php will be process at server and your browser will only see the output of it, if any.

javascript will be ignore by server and will be dump to your browser, and your browser will display it.

php will always be process/run first by server, and whatever the output along with anything outside the php tag will be dump to your browser.

the easy way to visualise your code is: replace everything in php tag to whatever the result of the php code is doing to plain text, that is what your browser will see.

you can always mix php and javascript.

you can even "write" javascript using php like:
echo "<script>document.write('hello');</script>";

but you cannot "write" php using javascript like this:
document.write("<?php echo 'hello'; ?>");

of course, the above example actually work because your browser will see:
document.write("hello");

that is because the statement
<?php echo 'hello'; ?>

has been processed at server and the output is
hello


As I'm also new to this javascript and PHP, this for this reply...!
badai
jmraker wrote:
Javascript isn't limited to browsers. Google's has separated the v8 javascript engine from the browser ( https://developers.google.com/v8/embed ) and can run as a simple server ( http://www.nodejs.org http://socket.io/ )

In PHP most of the time you'd just include a javascript file by outputting the tag
Code:
<script type="text/javascript" src="TheJavascriptFile.js"></script>
for every javascript file you need to include
and you can include inline javascript like
Code:
<script type="text/javascript">var name1="John Doe";</script>


While outputting the script tag you can output the value of variables or the return values of many of the built in php functions
Code:
<?php ...
echo '<script type="text/javascript">var object1=' . json_encode($object1) . ';</script>
...
?>

which will output the variable $object1 as an JavaScript Object Notation encoded string


know your audience.
Marcuzzo
I've heard of AJAX but this concept is new to me.
I'm not sure what the benefits are of using JavaScript on the server when you've already got PHP.
I'd say leave JS for the client and use PHP on the server.
sonam
The best combination between PHP and Javascript is Ajax. It is very good for interaction between user input and server without reloading page what is not at possible with pure PHP.

Sonam
badai
sonam wrote:
The best combination between PHP and Javascript is Ajax. It is very good for interaction between user input and server without reloading page what is not at possible with pure PHP.

Sonam


ajax is not limited to PHP. it will work with any server side stuffs, like ASP, C, jsp, etc. but ajax itself should be look at as client side javascript. it just script that is run by the browser after the page has been loaded.
inuyasha
Badai has already posted what I wanted to reply to you on reading your title~ Very Happy Javascript and PHP are not that related.
codersfriend
yeah ajax is very useful for combining javascript with server side scripts like php. You'll just have to use javascript to interact with php pages
cfvergara
Using ajax, as opposed to other possibilities -like, say, generating javascript with php- helps enforce something real cool called "separation of concerns", which basically means you can stop caring about the interaction itself (is this a PHP or javascript bug?) and start caring about each part of it independently from the other.

This in time starts being such an important part of your process that maybe you'll notice there's other things on each side that you might want to separate, thus eventually leading to either a healthy organization of code in layers, or an unholy mess that will make you reflect on how to do it better later.
jmraker
When you need a php variable used in your javascript instead of having the php script outputting/generating the entire javascript functions and all you can just have the php script generate some javascript initialization values like
Code:

<?php
...
echo '<script>
var phpsessid = "' . session_id() . '";
var uploadDirectory = "' . $config['UploadDir'] . '";
</script>
<script src="ScriptThatUsesAboveVariables.js"></script>
';
..
?>

Where it'd generate something like
Code:

<script>
var phpsessid = "1a2b3c4d1a2b3c4d1a2b3c4d';
var uploadDirectory = "/uploads/Files/";
</script>
<script src="ScriptThatUsesAboveVariables.js"></script>


That way the file ScriptThatUsesAboveVariables.js can become cached by the web browser and help reduce the network resources used for other things.
Laszer
greetings, , You asked for some examples for learning javascript, , My opinion is if you do web site development (make web sites) you will definitely need to know the basics for javascript! There are very many web pages-sites for learning javascript, here are two that you should look at to start learning javascript , at w3schools, has a great -try it yourself- feature that lets you see how it works, without writing your own test pages -
http://www.w3schools.com/js/default.asp


and at about.com -
http://javascript.about.com/library/blwrite.htm

javascript is not so difficult for the basics, but gets really mushy and confusing as you get to more difficult jobs. But you should learn if you can.
littlelamb749
AJAX is fantastic for live updates on the html end. I use it for search results that don't need to refresh the page or login screens that can login users without a refresh. The site http://www.newgrounds.com used to use AJAX to login however they have since then changed it.
Related topics
What computer languages you know?
Just Getting Started
Site
Jaki portal php polecacie?
Do you code HTML or do you use wysiwyg editors?
[DESIGNER] - Sites de Código HTML, JAVASCRIPT, PHP(PORUGUÊS)
Beste website van het jaar
what are the differents between script and language?
234 productions
which one programming lanuge result's the best speed
Google Image Ripper
Whos is the FUTURE.... JAVA or .NET
Layout Gap PRoblems with PHP/Javascript Combo
3D Worm in JavaScript!!!
javascript php html styling and good practice.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.