PDA

View Full Version : PHP: FORMulations


Horus_Kol
07-14-2005, 10:50 AM
Requirements:

1. Access to a server with PHP installed
2. A text editor to create the files
3. A working knowledge of HTML


A simple example on a method to handle forms (along with errors).

The error handling will be basic – just checking for empty inputs. A tutorial for validation will be available in the future.

Nothing will really be done with the information – no database connections or emails. The input will just be reprinted on the screen if successful.


The Files

I will make use of a modified index.php and page.php from the tutorial “My First PHP”. There is also a third file attached to this tutorial called form.php.

The Form

In the form.php file, we need to create the following function to create the form:


function form_DrawForm($f, $e, $a)
{
// draws a form on the page
// $f = previous form data
// $e = errors in previous submission
// $a = action for the form

?>
<form action='<?php echo $a; >' method='post'>
<!-- action = file used to handle the form -->
<!-- method = transmission method of form data
post = hidden in HTTP request
get = coded in the URL and is visible -->
<?php

if (isset($e['username']))
{
// if there is a problem with the previous input, then try again
// and highlight the problem
echo "<span style='color : #CC0000; font-weight : bold'>";
echo "You must enter a username";
echo "</span><br />\r\n";
}

?>
<input type='text' name='username' id='form_text_username'
class='form_text_input' value='<?php echo $f['username']; ?>' />

<br />

<button type='submit' name='submit' value='submit'>Submit</button>
</form>
<?php
}



Initialising the Form


<input type='text' name='username' id='form_text_username'
class='form_text_input' value='<?php echo $f['username']; ?>' />


We can create default values for the first presentation of the form (ideal for user profile configuration or similar).

All we need is another function to do this. This function will return an array of values:


function form_InitForm()
{
// initialises the inputs for the form

$f['username'] = ""; // empty string for the initial value

return $f;
}



Checking the Form

This function goes through the input information and returns an array of errors if they are found. Remember that this is only a simple validation of the input which only requires that something be entered – tutorials on more complex validation methods will be written soon.


function form_CheckForm($f)
{
// validates the inputs of the form
// $f = form inputs

$e = array();

if ($f['username'] == "")
{
$e['errors'] = true; // this must be set on all error conditions
$e['username'] = true; // allows us to specify an error within the username
}

return $e;
}



Handling the Form


function form_HandleForm($f)
{
// performs the actions of a successful form input

echo "Welcome ". $f['username'];
}


Scoutt has already written an email handler tutorial, and a database tutorial is not long in arriving to HTML Forums.


Putting It All Together

Okay, we got the functions… now what about the actual script?

Well, first off, we will need to include the file form.php into the script (index.php):


include "includes/index.php";


We’ll do the messy stuff like validation and/or initialisation before we send any content to the browser:


if (isset($_POST['submit']))
{
// checks if a form has been submitted already
// the form is using the POST method, so we check the $_POST array
$formdata = $_POST;
$errors = form_CheckForm($formdata); // validates the inputs
}
else
{
$formdata = form_InitForm(); // initialises the form
$errors = array(); // empty array
}


So the above code goes right before the page_Header() function used in the script.

The rest of the code goes into the content section (between page_MainStart() and page_MainFinish()):


if ((!isset($_POST['submit'])||(isset($errors['errors']))
{
// if no previous form has been submitted, or an error is found
form_DrawForm($formdata, $errors, "index.php");
}
else
{
form_HandleForm($formdata);
}


Well, there you go – a simple way to deal with forms.

serahayden
04-04-2007, 05:58 AM
hi i am a first timer making a web site and trying to make my form mail and when i do it wiher links to a page of script or a page from the guys who host my site so i gather i'm not doing it right even though it looks the same as over forms script, i have been trying to call the guys who host us and they won't answer so now i'm a little frustrated....can anyone help?

please!