Using Forms

April 13th, 2009 | Tags:

Tutorial Description:

Learn how to use form variables with PHP.

Lets say we have the following HTML form, simple enough, right?

<html>
<head>
<title>Using forms with PHP</title>
</head>
<body>
<form action="submit.php" method="post">
Name: <input type="text" name="name" size="25" />
<input type="submit" value="Submit!" />
</form>
</body>
</html>

Notice the attributes within the tag, the action part is telling the form to go to submit.php after the form is submitted, the method part is just defining the form type [post or get].

So now that we have that page, we need the submit.php page.

Once the form is submitted, it executes submit.php. The form input we are using is called “name”. After the form is submitted, we can access that variable with $_POST['name'].

<?php
/* File name: submit.php */

// store & clean the form variable.

$name = trim(strip_tags($_POST['name']));

// now we can play with it.
echo 'Hello world, my name is <b>' .$name. '</b> and I love SweDesignz!';

?>

As you can see above, we are using the submitted name in our script. You can do some more cool things. For example instead of entering a name, you can submit a number, and then play around with it.

This was just to show you how to use the form variables in your script.

No comments yet.
TOP