There are lot of tutorials online.
w3schools.com has excellent tutorials on both
PHP and
Javascript.
Quote:
Originally Posted by dragon52
What I was getting at before was I had a run time error "Error: member not found" where as when I ran Atli's code there was no error. What's missing in my code? The type="text/javascript" bit?
There are several problems in that code.
The one causing the error was most likely that you were trying to use the
body element before it was created.
This behavior is actually different in different browsers, but it seems that if you try to assign something to
document.body, IE gives you that error.
Firefox doesn't complain tho.
There is also the fact that you are trying to re-create the body tag, which is generally not a very good idea. Use the existing one if you must, but I wouldn't try to create a new one.
You also forgot the
type attribute on the <script> tag.
This tells the browser what kind of script the tag contains. Most browsers default to the latest Javascript version, but it is best not to assume that.
(Hint: Don't use IE! Use a standard supporting browser, like Firefox. Test your code on that and then when your done, make sure it works in IE to.) Quote:
Now another question is where should I place the php and script code? Only after the php code to successfully update the database, for example, that I need to build and tailor the feedback for individual users, like "Hello Mr xxx. all done!"
You place PHP code... wherever it is needed.
Keep in mind that PHP is
not a part of the website. It is what creates the website.
PHP speaks HTML. It generates the HTML on the server and then sends it to the browser. The browser never knows PHP had anything to do with creating the HTML. So it doesn't really matter where your PHP code is, as long as it generates valid HTML.
Like, if you put this into a PHP page:
-
<?php $username = 'John Doe'; ?>
-
<html>
-
<head><title>Test</title></head>
-
<body>
-
<?php echo "Hello, {$username}.";
-
</body>
-
</html>
And then visited that PHP page in your browser, all your browser would get is:
-
<html>
-
<head><title>Test</title></head>
-
<body>
-
Hello, John Doe.
-
</body>
-
</html>
The browser never knows PHP had anything to do with putting the name there.
It just displays the HTML, ignorant of it's origins.
Javascript, on the other hand,
is a part of the website.
It is executed by the browser, long after the PHP code has been executed.
You should always try to put your Javascript code inside the <header> tag, like I did, or better yet, into external Javascript files.
For example:
-
/**
-
* File: script.js
-
*/
-
function SayHello() {
-
alert("Hello, World!");
-
}
-
<!--
-
- File: index.html
-
-->
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html>
-
<head>
-
<title>External Javascript Test</title>
-
<script type="text/javascript" src="script.js"></script>
-
</head>
-
<body>
-
<button onclick="SayHello();">Say Hello</button>
-
</body>
-
</html>
Clicking the button would then bring up a alert window with the message
"Hello, World!"