Home arrow PHP arrow Page 3 - Validating Web Forms with the Code Igniter PHP Framework

Displaying error messages when validating an online form - PHP

If you're a web developer, you've probably written any number of scripts designed to validate the input entered into a web form. More than likely, then, you'd like something that will make that job easier. Enter the Code Igniter PHP framework. In this fourth part of a nine-part series on this framework, you'll learn how to use it to do basic validation on a web form.

  1. Validating Web Forms with the Code Igniter PHP Framework
  2. Validating user-supplied data using the MVC pattern
  3. Displaying error messages when validating an online form
  4. Building a confirmation view file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
September 03, 2008

print this article



In the previous section, you learned how to use a controller class to determine what course of action to take, either when the form in question has been submitted correctly, or when it fails to pass the validation process.

Now Iím going to show you how to create the view file that displays the pertinent online form, and incidentally, it also shows an error message as a result of the data checking process.

That being explained, please examine the code below to see how this error view file looks. 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Entering user data</title>



<h1>Entering user data</h1>

<?php echo $this->validation->error_string;?>

<?php echo form_open('validator');?>

<p>First Name <input type="text" name="firstname" value="" size="50" /></p>

<p>Last Name <input type="text" name="lastname" value="" size="50" /></p>

<p>Email <input type="text" name="email" value="" size="50" /></p>

<p><input type="submit" value="Send Data" /></p>




As shown above, the previous view file is quite simple to follow. As you can see, itís comprised of a basic online form that contains three text boxes for collecting the userís first and last names, and the corresponding email address.

However, you should notice two important things regarding the way that the above contact form is created. First, a PHP statement has been included at the beginning of it. As you may guess, this is the error message that will be  generated by the Code Igniter validation class when the web form fails to pass the validation process.

Finally, the view file uses the form helper function loaded previously by the controller to create a <form> opening tag that points exactly to the controllerís location. Naturally, itís possible to build this tag manually, but using the helper makes sure that the correct URL will always be assigned to the ďactionĒ attribute of the online form.

So far. So good. At this stage, I taught you how to build a simple view file for showing a basic error message when validating a specific web form. In addition, the view will redisplay the form in question if any of its fields has been left empty, so users can repopulate the offending fields with the correct data and resubmit it.

So, now that you hopefully learned how the previous view file does its thing, save it to the Code Igniter /system/application/views/ folder, and move on to see how to create the view that displays a primitive successful web page when the pertinent web form is submitted correctly.

To learn the full details for how this brand new view file will be built, please jump ahead to read the next section. 

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: