|
Posted by Toby Inkster on 02/27/06 11:01
senhortomas wrote:
> Can html perform interactively or make decisions?
No -- you'd need either a server-side or client-side programming language.
ECMAScript (a.k.a. Javascript) is really the only client-side scripting
language in town. Here's an example of something you could do with it:
<script type="text/javascript">
function sum ()
{
var A = document.getElementById('a');
var B = document.getElementById('b');
var C = document.getElementById('c');
C.value = A.value + B.value;
}
</script>
<div>
<input id="a"> +
<input id="b">
<input type="button" onclick="sum();" value="=">
<input id="c">
</div>
Server-side languages are more reliable in that they don't rely on browser
support -- the server does the hard work. The draw-back is that for
the browser has to load a new page to get the results, so to the end user,
the experience is slower.
You can potentially use any programming language that you like for
server-side scripts -- you just need to make sure your server is able to
run programmes in that language. Common choices are Perl, PHP, VBScript,
JScript, Java and Python, though C, C++, Shell, Fortran et al can be used
equally well.
Here is an example that performs a similar function to the script above,
but written in PHP:
<?php
$A = $_GET['a'];
$B = $_GET['b'];
if (isset($A) && isset($B))
$C = $A + $B;
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="GET">
<div>
<input name="a" value="<?=$A?>"> +
<input name="b" value="<?=$B?>">
<input type="submit" value="=">
<input name="c" value="<?=$C?>">
</div>
</form>
By combining client- and server-side programming, you can have the best of
both worlds: near-instantaneous results (thanks to client-side), with a
reliable fall-back.
Example:
<?php
$A = $_GET['a'];
$B = $_GET['b'];
if (isset($A) && isset($B))
$C = $A + $B;
?>
<script type="text/javascript">
function sum ()
{
// If this browser doesn't support
// getElementById, then fall back to
// PHP.
if (!document.getElementById)
return true;
// Add the numbers up and display
// the result.
var A = document.getElementById('a');
var B = document.getElementById('b');
var C = document.getElementById('c');
C.value = A.value + B.value;
// Cancel the PHP script.
return false;
}
</script>
<form onsubmit="return sum();"
action="<?=$_SERVER['PHP_SELF']?>"
method="GET">
<div>
<input id="a" name="a" value="<?=$A?>"> +
<input id="b" name="b" value="<?=$B?>">
<input type="submit" value="=">
<input id="c" name="c" value="<?=$C?>">
</div>
</form>
(Note: the scripts above are a little simplistic. In real life you'd
probably want to check that A and B are actual numbers before trying to
add them together. Adding "12" to "x" will result in "12x" in Javascript,
but "12" in PHP!)
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
[Back to original message]
|