Pear::html

Source: Internet
Author: User
Tags empty modify variables pear quickform administrator password smarty template

Haohappy in the "PHP & More" in the third "Pear::html_quickform" in the article said to write Quickform and smarty of the combination of application, has not written, today saw PHPE forum friends in the inquiry, Ashamed of being lazy. Now make up, I hope to help you a little. In my opinion, Pear::html_quickform is a very good form library, greatly accelerated the development speed, most of my current projects will be used. If you are not familiar with the Pear::html_quickform friend, suggest that read this article first.

This article is aimed at the reader for the rich development experience of PHP programmers, asking readers

<!--[if!supportlists]-->1. <!--[endif]--> familiar with pear and its installation and use;

<!--[if!supportlists]-->2. <!--[endif]--> familiar with Html_quickform;

<!--[if!supportlists]-->3. <!--[endif]--> understands the concept of templates and is familiar with the use of Smarty template engines.

In the "Pear::html_quickform Primer" form of the landscaping output section, refers to the use of the quickform with the form of decoration methods to beautify the output. Obviously, this approach seems a bit cumbersome, and it's a bit hard for programmers to beautify the web. Now the most common collaboration between programmers and designers is through templates, so how to combine the Quickform with the template engine is the problem we need to solve. In fact, Quickform can be combined with a variety of template engines, such as ITX, Sigma, Flexy, Smarty, and so on, each template has its advantages and disadvantages, the current smarty is the most common template engine, So we focus on the combination of Quickform and smarty.

First of all, let's look at our final results:

This example is very simple, only one form,4 input, only to explain the use of quickform. In the actual development, we often encounter dozens of input of the situation. In fact, the more complex the form, the more it shows the inefficiency of our traditional approach, the more powerful the quickform. This, perhaps you will realize later.

OK, let's start our quickform+smarty trip.

changpwd.php

?
Require_once ("includes/config.inc.php");
Building Smarty Objects
$smarty = new Smarty_app;
$smarty->assign (' Cssdir ', './templates/admin ');
$smarty->assign (' title ', ':: Haohappy Test Website Management system:: ');

Building a login form
$form = new Html_quickform (' frmchgpwd ', ' post ');

Add form elements
$form->addelement (' Password ', ' adminpwd ', ', ', ' class = Nameandpwd ');
$form->addelement (' Password ', ' newpwd ', ', ', ' class = Nameandpwd ');
$form->addelement (' Password ', ' newPwd2 ', ', ', ' class = Nameandpwd ');
$form->addelement (' Submit ', ' btnsubmit ', ' Change Password ', ' class = Btnsubmit ');

Adding validation rules automatically generates JavaScript variables, which are stored in JavaScript validation functions
$form->addrule (' adminpwd ', ' Password cannot be empty! ', ' required ', ', ' client ');
$form->addrule (' newpwd ', ' New password cannot be empty! ', ' required ', ', ' client ');
$form->addrule (' newPwd2 ', ' New password cannot be empty! ', ' required ', ', ' client ');
$form->addrule (Array (' newpwd ', ' newPwd2 '), "the password entered two times is different!! ", ' compare ', '", ' client ');

if ($form->validate ()) {
If the form data is correct, modify the password
$form->process (' changepwd ');
}
else{

Otherwise display the form

Create a Renderer Object
$renderer =& New Html_quickform_renderer_arraysmarty ($smarty);

Build the HTML for the form to generate the HTML code for the forms
$form->accept ($renderer);

Assign array with form data allocation form to array
$smarty->assign (' Form_data ', $renderer->toarray ());
$smarty->catching = false;

Debugging
echo "<pre>"; Var_dump ($renderer->toarray ()); echo "</pre>";
$smarty->display ("Changepwd.tpl");

}

Modify password
function Changepwd () {}
?>

In the code, we added 4 form elements with $form->addelement (), adding 4 validation rules with $form->addrule (). How, is it quick and convenient? To verify that two passwords are the same validation rules for example, if we write the validation rules ourselves, although fast, but the code will appear bloated and messy, by Quickform to be responsible for data validation, development speed greatly improved, and the code looks very concise beautiful. Only one line of code is used:

$form->addrule (Array (' newpwd ', ' newPwd2 '), "the password entered two times is different!! ", ' compare ', '", ' client ');

For the benefit of quickform, please see "Getting Started with Pear::html_quickform," which is not repeated here.

The following line of code is the bridge that we will connect Html_quickform with Smarty:

$renderer =& New Html_quickform_renderer_arraysmarty ($smarty);

The so-called renderer, is used for display, where we quickform renderer designated as Smarty, we can use a powerful Smarty template engine to format the output of the quickform.

Other:

Changepwd () is the core operating function of this file to modify the password.

$form->process (' changepwd '); This line of code is used to invoke Changepwd ()


echo "<pre>"; Var_dump ($renderer->toarray ()); echo "</pre>";

This line is used for debugging, and we can always print out all the variables in the $renderer to see if the program executes correctly.

Look at our template, also very simple:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<TITLE> CHANGEPWD.TPL </TITLE>

<meta name= "generator" content= "EditPlus" >

<meta name= "Author" content= "Haohappy" >

<meta name= "Keywords" content= "" >

<meta name= "Description" content= "" >

<link href= "{$CSSDIR}/style.css" rel= "stylesheet" type= "Text/css" >

{if $form _data.javascript}

{$form _data.javascript}

{/if}

</HEAD>

<BODY>

<p> </p>

<p> </p>

<p> </p>

<form {$form _data.attributes} >

<table width= "border=" 0 "align=" center "cellpadding=" 3 "cellspacing=" 3 "bgcolor=" #F6F6F6 "style=" font-size : 9pt "class=" addtable ">

<tr bgcolor= "#FFFFFF" >

<TD width= "47%" colspan= "2" ><div align= "Center" > Modify Admin password </div></tr>

<tr>

<tr>

<TD width= "47%" ><div align= "Center" > Existing Administrator password

</div></td>

<TD width= "53%" >{$form _data.adminpwd.html}</td>

</tr>

<tr>

<td><div align= "center" > New password

</div></td>

<td>{$form _data.newpwd.html}</td>

</tr>

<tr>

<td><div align= "Center" > Enter new password again

</div></td>

<td>{$form _data.newpwd2.html}</td>

</tr>

<tr>

<TD colspan= "2" ><div align= "center" >

{$form _data.btnsubmit.html}

</div></td>

</tr>

</table>

</form>

</BODY>

</HTML>

These two simple files, a total of less than 100 lines of code, completed the beginning of our article effect. Contains the complete form data validation, processing process.

Another: The use of quickform, you can easily achieve the display layer and the separation of the logical layer, because the processing function is completely independent.

For example, you can put

if ($form->validate ()) {
If the form data is correct, modify the password
$form->process (' changepwd ');
}
<!--[If!supportlinebreaknewline]-->
<!--[endif]-->

Modified into

if ($form->validate ()) {

Switch ($post _vars[' action ']) {

Default

Case "Changpwd":

$form->process (' changepwd ');

Break

Case "ADD":

$form->process (' Add ');

Break

Case "Update":

$form->process (' Update ');

Break

Case "Delete":

$form->process (' delete ');

Break

}
<!--[If!supportlinebreaknewline]-->
<!--[endif]-->

The Changepwd,add,update,delete four functions are then separated into a file. This allows different actions to be invoked based on the action that the page commits.

The idea is relatively rudimentary, and if you want to use more powerful features, you can try it.

Pear::html_quickform_controller. Html_quickform_controller is based on the Pagecontroller design pattern, which uses a single page to handle request and action from Get and post delivery. This is a very interesting idea, but the development model is not suitable for beginners because it is relatively complex. The author also said:

Html_quickform_controller is isn't intended for PHP newbies. If you don ' t understand what classes are, if you have no prior experience with Quickform, if you are a fan of copy-paste p Rogramming then this package isn't for you.

The package is indeed complex, but so are the problems it are trying to solve. Try to rewrite any of the enclosed multipage form examples without using such a package and you'll be in what we mean.

This part is not discussed, this requires you to have a certain understanding of MVC, the next opportunity to write another article. My own current development framework is an MVC framework, feel very comfortable, show part is to Smarty+quickform to complete. Hope that more people pay attention to and use of quickform, I believe that after the use of people will not be disappointed



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.