$this->load->helper('form');
創建一個開始form標簽,相對于你的配置文檔中的基礎URL。
允許你添加一些form屬性和一些隱藏表單,并且他會基于你的 config.php 文件里設置的編碼,自動生成 accept-charset 這個屬性。使用這個函數而不是直接硬編碼HTML的主要的優勢是使你的程序可以方便的轉換,如果你的URL變化的話。
echo form_open('email/send');上面的例子會創建一個form提交至你的基礎URL加上"email/send" URI片段,像這樣:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" />
添加一些屬性
可以在第二個參數里傳遞一個關聯數組來達到這一目的, 像這樣:
$attributes = array('class' => 'email', 'id' => 'myform');echo form_open('email/send', $attributes);上面的例子會創建一個這樣的form標簽:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="myform" />
增加隱藏域
隱藏域可以使用數組加在第三個參數上,就像這樣:
$hidden = array('username' => 'Joe', 'member_id' => '234');echo form_open('email/send', '', $hidden);上面的例子會創建一個這樣的form標簽和這些隱藏域:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send"><input type="hidden" name="username" value="Joe" /><input type="hidden" name="member_id" value="234" />
form_open_multipart()
這個函數和上面的form_open()函數完全一樣,不同之處在于它多了一個multipart屬性。如果你要制作一個上傳文件的表單,這個屬性是必須的。
echo form_hidden('username', 'johndoe');將產生:
<input type="hidden" name="username" value="johndoe" />或者你也可以使用數組來聯合創建它們:
$data = array( 'name' => 'John Doe', 'email' => 'john@example.com', 'url' => 'http://example.com' );echo form_hidden($data);將產生:
<input type="hidden" name="name" value="John Doe" /><input type="hidden" name="email" value="john@example.com" /><input type="hidden" name="url" value="http://example.com" />
echo form_input('username', 'johndoe');或者你也可以用關聯數組來添加你想加入的內容:
$data = array( 'name' => 'username', 'id' => 'username', 'value' => 'johndoe', 'maxlength' => '100', 'size' => '50', 'style' => 'width:50%', );echo form_input($data);將產生:
<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />如果你想加入一些額外的內容,例如Javascript,你可以在第三個參數里輸入字符串來創建它:
$js = 'onClick="some_function()"';echo form_input('username', 'johndoe', $js);form_password()
此函數除了是設置type為“password”外和上面的 form_input() 函數完全一樣。
此函數與上面的 form_input() 函數幾乎完全相同,唯一的區別是此函數所設置的 type 為 "file",用來上傳文件。
此函數與上面的 form_input() 函數幾乎完全相同,唯一的區別是此函數所生成的是一個"textarea"。說明:上面的范例中所指定的 "maxlength" 和 "size" 屬性在這里變成了 "rows" 和 "cols"。
$options = array( 'small' => 'Small Shirt', 'med' => 'Medium Shirt', 'large' => 'Large Shirt', 'xlarge' => 'Extra Large Shirt', );$shirts_on_sale = array('small', 'large');echo form_dropdown('shirts', $options, 'large');將會生成:
<select name="shirts"><option value="small">Small Shirt</option><option value="med">Medium Shirt</option><option value="large" selected="selected">Large Shirt</option><option value="xlarge">Extra Large Shirt</option></select>
echo form_dropdown('shirts', $options, $shirts_on_sale);將會生成:
<select name="shirts" multiple="multiple"><option value="small" selected="selected">Small Shirt</option><option value="med">Medium Shirt</option><option value="large" selected="selected">Large Shirt</option><option value="xlarge">Extra Large Shirt</option></select>如果你希望 <select> 開始標簽包含一些額外的屬性,例如 id 屬性以及JavaScript,你可以將一個字符串作為第四個參數傳遞過去:
$js = 'id="shirts" onChange="some_function();"';echo form_dropdown('shirts', $options, 'large', $js);
如果$options參數是一個多維數組,form_dropdown() 函數將使用數組的鍵作為 label值生成一個 <optgroup> 標簽。
form_fieldset()
幫助你生成fieldset/legend標簽。echo form_fieldset('Address Information');echo "<p>fieldset content here</p>";echo form_fieldset_close();生成
<fieldset><legend>Address Information</legend><p>form content here</p></fieldset>
與其它函數類似,在第二個參數你可以傳遞一個關聯數組來添加你自己想要的自定義的屬性。
$attributes = array('id' => 'address_info', 'class' => 'address_info');echo form_fieldset('Address Information', $attributes);echo "<p>fieldset content here</p>";echo form_fieldset_close();生成
<fieldset id="address_info" class="address_info"><legend>Address Information</legend><p>form content here</p></fieldset>
$string = "</div></div>";echo form_fieldset_close($string);會生成:
</fieldset></div></div>
echo form_checkbox('newsletter', 'accept', TRUE);將會生成:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />第三個參數為 TRUE/FALSE 布爾值,決定復選框是否被默認選中。
$data = array( 'name' => 'newsletter', 'id' => 'newsletter', 'value' => 'accept', 'checked' => TRUE, 'style' => 'margin:10px', );echo form_checkbox($data);將會生成:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />與其它函數相似的,如果你希望此標簽包含額外的數據,例如JavaScript,你可以將一個字符串作為第四個參數傳遞過去:
$js = 'onClick="some_function()"';echo form_checkbox('newsletter', 'accept', TRUE, $js);
form_radio()
此函數與上面的 form_checkbox() 函數幾乎完全相同,唯一的區別是此函數生成的是 "radio" 單選框。
echo form_submit('mysubmit', 'Submit Post!');會生成:
<input type="submit" name="mysubmit" value="Submit Post!" />
與其它函數類似,第一個參數你可以傳遞一個關聯數組來設置你所需要的屬性。第三個參數允許你添加一些額外的內容到生成的HTML代碼里。
echo form_label('你的名字是?','username');會生成:
<label for="username">你的名字是?</label>與其它函數類似,你可以在第三個參數傳入一個關聯數組來增加一些額外的屬性值。
$attributes = array('class' => 'mycustomclass','style' => 'color: #000;',);echo form_label('你的名字是?', 'username', $attributes);會生成:
<label for="username" class="mycustomclass" style="color: #000;">你的名字是?</label>
作用是生成一個標準的 reset 按鈕。此函數用法與 form_submit() 完全相同。
echo form_button('name','content');將會生成
<button name="name" type="button">Content</button>或者你也可以將一個包含有任何你想要的數據的關聯數組作為參數傳遞過去:
$data = array( 'name' => 'button', 'id' => 'button', 'value' => 'true', 'type' => 'reset', 'content' => 'Reset');echo form_button($data);將會生成:
<button name="button" id="button" value="true" type="reset">Reset</button>如果你希望表單包含一些額外的數據,例如JavaScript,你可以將一個字符串作為第三個參數傳遞過去:
$js = 'onClick="some_function()"';echo form_button('mybutton', 'Click Me', $js);
$string = "</div></div>";echo form_close($string);將會生成:
</form></div></div>
$string = 'Here is a string containing "quoted" text.';<input type="text" name="myform" value="<var<$string</var<" />因為上面的字符串中包含了引號,因而導致表單被破壞。form_prep()函數會轉換HTML,因此可以放心使用:
<input type="text" name="myform" value="<var<<?php echo form_prep($string); ?></var<" />
說明: 如果你使用的是表單輔助函數中的任何一個,數據都會自動的進行預處理,所以沒有必要調用本函數。只有當你手動創建表單元素時,你才需要本函數。
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
當上面的表單元素第一次加載時將會顯示"0"。
<select name="myselect"><option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option><option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option><option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option></select>
set_checkbox()
允許你顯示一個處于提交狀態的復選框。第一個參數必須包含此復選框的名稱,第二個參數必須包含它的值,第三個參數是可選的,作用是設置復選框為默認選中狀態(使用TRUE/FALSE布爾值)。例如:<input type="checkbox" name="mycheck" value="1" <?php echo set_checkbox('mycheck', '1'); ?> /><input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck', '2'); ?> />
允許你顯示那些處于提交狀態的單選框。這個函數與前面的 set_checkbox() 是相同的。
<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> /><input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />