PHP On-The-Fly!
(Page 3 out of 4)Let's try a more advanced example. In the following example, the visitor can enter two numbers, and they are added up by PHP (and not by JavaScript). This shows the true power of PHP and the XML HTTP Request Object.
This example uses the same script.js as in the first example, so you don't need to create this again. First, copy the code below and paste it in a file called 'server2.php':
// Get numbers
$num1 = intval($_GET['num1']);
$num2 = intval($_GET['num2']);
// Return answer
echo ($num1 + $num2);
?>
And then, copy the code below, and paste it in a file called 'client2.php'. Please note though that you need to edit the line that says 'http://www.yourdomain.com/server2.php' to the correct location of server2.php on your server.
Use the below form to add up two numbers. The answer is calculated by a PHP script, and not with JavaScript. What's the advantage to this? You can execute server-side scripts (PHP) without having to refresh the page.
type="text" id="num1" size="3" /> + type="text" id="num2" size="3" /> = type="text" id="answer" size="5" />
type="button" value="Calculate!" OnClick="calc();" />
When you run this example, you can add up two numbers, using PHP and no reloading at all! If you can't get this example to work, then have a look on http://phpit.net/demo/php%20on%20the%20fly/client3.php to see the example online.