PHP automatically generated form code sharing

Source: Internet
Author: User
During PHP development, we sometimes encounter forms that are not fixed and need to be dynamically generated during the program running. here we will share a class for PHP to automatically create forms for everyone. php.

<? Php // Form. phpclass form {var $ layout = true; // whether to use table layout var $ action; // URL var $ method to be submitted to the form; var $ enctype = ""; var $ name = ""; var $ id = ""; var $ class = ""; function form ($ action, $ method = "POST ") {// initialize the member variable $ this-> action = $ action; $ this-> method = $ method;} function form_start () through the constructor () {$ text = "\ n";} else {$ text ="\ N ";}return $ text ;}// function of the text box function form_text ($ name, $ id, $ label_name, $ label_for, $ value =" ") {$ text ="Form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text); return $ form_item ;} // function form_passwd ($ name, $ id, $ label_name, $ label_for, $ value = "") {$ text ="Form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text); return $ form_item ;} // hide the domain function form_hidden ($ name, $ id, $ label_name, $ label_for, $ value = "") {$ text ="Form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text); return $ form_item ;} // file domain function form_file ($ name, $ id, $ label_name, $ label_for, $ size = "") {$ text ="Form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text); return $ form_item;} // check box function form_checkbox ($ name, $ label = array (), $ label_name, $ label_for = "") {$ I = 0; $ text = array (); foreach ($ label as $ id => $ value) {$ text [$ I] =""; $ Text [$ I]. ="{$ Value}"; $ I ++;} $ label = $ this-> form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text ); return $ form_item;} // function form_radio ($ name, $ label = array (), $ label_name, $ label_for = "") {$ I = 0; $ text = array (); foreach ($ label as $ id => $ value) {$ text [$ I] =""; $ Text [$ I]. ="{$ Value}"; $ I ++;} $ label = $ this-> form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text ); return $ form_item;} // the drop-down menu function form_select ($ id, $ name, $ options = array (), $ selected = false, $ label_name, $ label_for, $ onchange = "") {if ($ onchange! = "") {$ Text ="\ N ";}else {$ text ="\ N ";} foreach ($ options as $ value => $ key) {if ($ selected = $ value) {$ text. =" \ t{$ Key}\ N ";} elseif ($ selected = false) {$ text. =" \ t{$ Key}\ N ";}}$ text. =""; $ Label = $ this-> form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text); return $ form_item ;} // function form_selectmul ($ id, $ name, $ size, $ options = array (), $ label_name, $ label_for) {$ text ="\ N "; foreach ($ options as $ value => $ key) {$ text. =" \ t{$ Key}\ N ";}$ text. ="\ N "; $ label = $ this-> form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text ); return $ form_item;} // button function form_button ($ id, $ name, $ type, $ value, $ onclick = "") {$ text ="Layout = true) {$ form_item ="\ N \ t {$ Text}\ N\ N ";}else {$ form_item = $ text;} return $ form_item;} // function of the text domain function form_textarea ($ id, $ name, $ cols, $ rows, $ label_name, $ label_for, $ value = "") {$ text ="{$ Value}\ N "; $ label = $ this-> form_label ($ label_name, $ label_for); $ form_item = $ this-> form_item ($ label, $ text ); return $ form_item;} // The function form_label ($ text, $ for) {if ($! = "") {$ Label ="{$ Text }:";} Else {$ label = $ text. ":";} return $ label;} function form_item ($ form_label, $ form_text) {switch ($ this-> layout) {case true: $ text ="\ N "; $ text. =" \ t"; $ Text. = $ form_label; $ text. ="\ N "; $ text. =" \ t"; $ Text. = $ form_text; $ text. ="\ N "; $ text. ="\ N "; break; case false: $ text = $ form_label; $ text. = $ form_text; break;} return $ text;} function CreateForm ($ form_item = array () {echo $ this-> form_start (); foreach ($ form_item as $ item) {echo $ item;} echo $ this-> form_end () ;}}?>

Example:

    
     
     User logon<? Phprequire_once ("form. php "); $ form = new form ($ _ SERVER ['php _ SELF ']); // submit to this page $ form-> layout = false; // no table layout is used. you can comment out this sentence to see how the result is different. $ name = $ form-> form_text ("userid", "userid", "username ", "userid"); $ passwd = $ form-> form_passwd ("passwd", "passwd", "password", "passwd "); $ submit = $ form-> form_button ("", "submit", "submit", "logon"); $ form_item = array ($ name, $ passwd, $ submit ); $ form-> CreateForm ($ form_item);?>

The above is all the content of this article. I hope you will like it.

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.