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">
<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.
blog comments powered by Disqus