PHP and javascript are technically both scripting languages, not programming languages. A scripting language is a language that is compiled during run-time, whereas a programming language is typically compiled before run-time. What this means, is that for both PHP and javascript, the text files that contain the code sit on the server until a webpage is requested. When the webpage is requested, the PHP code is sent to the PHP interpreter, processed, and then sent to the user. When the user's client (web browser) gets the page, the client runs the javascript code though the javascript interpreter. For typical programming languages like C & C++, the code that is written is first compiled into an executable file of some sort (.exe file on windows machines), and then the executable file is run on the computer. This makes running the program much quicker, and is why PHP and javascript are not good languages for complex computation.
Examples:
Examples:
How do we create JavaScript?
The simplest way of incorporating javascript, which can be used anywhere in the HTML document where you can place an element is:
<script type="text/javascript"> /* <![CDATA[ */ // Insert javascript here. /* ]]> */ </script>
(For those with javascript experience who find this form unusual, the justification for this form is covered nicely in the article Javascript and XHTML.)
Your first JavaScript:
Now add the code in red to the document, so that it looks something like this:
<html> <head> <title>Hello world!</title> </head> <body> <h1>Hello Wonderful World</h1><script type="text/javascript"> /* <![CDATA[ */ alert("Hello World"); /* ]]> */ </script></body> </html>
Your second JavaScript:
Some cooler JavaScript:
Now edit the script element so that it contents are replaced by the text in red:
<html> <head> <title>Hello world!</title> </head> <body> <script type="text/javascript"> /* <![CDATA[ */var hello = document.createElement("h1"); var msg = document.createTextNode("Hello World"); hello.appendChild(msg); document.body.appendChild(hello);/* ]]> */ </script> </body> </html>
Helloworld Autopsy:
Let’s read this from right to left:
var hello = document.createElement("h1");
Now this one, from right to left again:
var msg = document.createTextNode("Hello World");
Again:
hello.appendChild(msg);
Finally:
document.body.appendChild(hello);
Let us pause for a minute, and note some general concepts which apply to both javascript and php:
Let's now see what functions and accessing javascript via links looks like.
Add a hyperlink in the body
<a href="#">Say Hello</a>
Now add a function definition as follows:
<html> <head> <title>Hello world!</title> <script type="text/javascript"> /* <![CDATA[ */function sayHello() {var hello = document.createElement("h1"); var msg = document.createTextNode("Hello World"); hello.appendChild(msg); document.body.appendChild(hello);}/* ]]> */ </script> </head> <body> <a href="#" onClick="sayHello()">Say Hello</a> </body> </html>
How do we create PHP?
It's very simple, and can be used anywhere in the HTML document:
<?php /*insert php code here */ ?>
However, in order for the php interpreter to process the php you have included in the file, you need to have a file of extension .php
Your first PHP:
Now add the code in red to the document, so that it looks something like this:
<html> <head> <title>Hello world!</title> </head> <body> <h1>Hello Wonderful World</h1><?php $tmp = "and I say hello!</p>"; echo "<p>You say goodbye, "; echo $tmp; ?></body> </html>
Your second PHP:
Edit the file so that the variable is defined in the <head> section, e.g.:
<html> <head><?php $tmp = "and I say hello!</p>"; ?><title>Hello world!</title> </head> <body> <h1>Hello Wonderful World</h1> <?php echo "<p>You say goodbye,;"; echo $tmp; ?> </body> </html>
Some cooler PHP:
Insert the following php right below the open <body> tag (insert spot #1):
<?php $frm_from = (int)$_POST['from']; if ($frm_from) { $frm_txt = htmlspecialchars($_POST['arbstuff']); echo "\t\t<p>\n"; echo "\t\t\tThe text you entered last time:\n"; echo "\t\t</p>\n"; echo "\t\t<p>\n"; echo "\t\t", $frm_txt, "\n"; echo "\t\t</p>\n"; } else { $frm_txt = "This text is not arbitrary unless you write something here..."; } ?>
Insert the following php in insert spot #2:
<?php echo $frm_txt; ?>
To turn in your assignment, please upload each file you created to your I: Drive. Then create a new HTML file in your I: drive that links to each of the files you created, and describe what you learned in this exercise (250-500 words) and email me a link to this file. Hint: it is probably a good idea to directly answer all the questions above if you want full credit.
Out of 30 points total.