Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > All Around Tutorials > Serverside Scripting Tutorials
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 07-14-2005, 10:50 AM
  #1
Horus_Kol
Mod of the Underlay
 
Horus_Kol's Avatar
 
Join Date: Jun 2002
Location: At a desk, hooked up and ready to rock
Posts: 17,242
iTrader: (0)
Horus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of lightHorus_Kol is a glorious beacon of light
PHP: FORMulations

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:

PHP Code:
  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

Code:
<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:

PHP Code:
  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.

PHP Code:
  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

PHP Code:
  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):

PHP Code:
  include "includes/index.php"
We’ll do the messy stuff like validation and/or initialisation before we send any content to the browser:

PHP Code:
  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()):

PHP Code:
  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.
Attached Files
File Type: txt index.php.txt (1.2 KB, 116 views)
File Type: txt page.php.txt (2.4 KB, 105 views)
File Type: txt form.php.txt (1.9 KB, 117 views)
__________________
Personal Blog (and photos): HorusKol
Articles on Programming and Development (PHP/HTML/CSS, C/C++, more): RandomTweak

The great secret that no SEO agent wants you to hear: if you build your website using w3c accessibility guidelines and your content is written for people, you will do better for longer in search engines than any other method...
Horus_Kol is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-04-2007, 05:58 AM
  #2
serahayden
Novice (Level 1)
 
Join Date: Apr 2007
Posts: 1
iTrader: (0)
serahayden is an unknown quantity at this point
help please

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!
serahayden is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 07:09 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.