Cameron Garnett/Testpage12

From WikiEducator
Jump to: navigation, search
PHP

Super Quick PHP Tutorial

Introduction | Basic Syntax | Sending Data to the Browser | Comments | Variables | Strings | Functions | Quotes | Debugging | Form


VmvIcon Objectives.png

Learning Objectives

By the end of this page you will know how to extract information from a html form and print it using php code.

Forms

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts.


VmvIcon Example.png

Example

<!DOCTYPE html>
<html>
<head></head>
<body>
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>

VmvIcon Objective.png

ACTIVITY

Write the above block of code into Notepad++ and save as form.html


VmvIcon Example.png

Example

Hi <?php echo htmlspecialchars($_POST['name']);?>.
You are <?php echo (int)$_POST['age'];?> years old.

VmvIcon Objective.png

ACTIVITY

Write the above block of code into Notepad++ and save as action.php into your web folder

For your browser to parse the form it has to be opened by typing a url similar to http://localhost/form.html
When you hit the submit button your browser will parse the information you have entered into the form and display the message in the action.php file.