To be frank, creating the view file responsible for displaying all the errors that might occur when validating the sample web form, and for repopulating its fields when applicable, is a straightforward process that you’ll grasp very quickly.
However, to clarify how this view is going to work, please examine its corresponding signature, which is shown below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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="<?php echo $this->validation->firstname;?>" size="50" /></p>
<p>Last Name <input type="text" name="lastname" value="<?php echo $this->validation->lastname;?>" size="50" /></p>
<p>Email <input type="text" name="email" value="<?php echo $this->validation->email;?>" size="50" /></p>
<p><input type="submit" value="Send Data" /></p>
Definitely, that wasn’t rocket science, was it? As shown before, the above view file is responsible for displaying a simple contact form, which not surprisingly is composed of three text boxes, which come in handy for entering typical data, such as the user’s first and last names, and an email address as well.
Nevertheless, if you have a closer look at the view file in question, then you’ll see that on top of it, the Code Igniter validation class is used to display all of the errors that might arise when validating the web form. Other than that, there are a few additional PHP statements interspersed with the structural markup, which are used to repopulate the form’s fields and to generate dynamically the URL for its “action” attribute.
Now you have learned how to build a simple view file that not only will be capable of displaying on screen all of the error messages that occurred when validating the sample HTML form, but will refill its fields with the data entered previously by a user.
Therefore, save this file to the Code Igniter /system/application/views/folder as “webform_view.php” and once you’ve done this, please jump forward and read the following section. In that part, I’ll explain how to build the remaining view file, which will be responsible for showing a confirmation message, after the web form has been submitted successfully.
blog comments powered by Disqus