$this->load->helper('form');
創(chuàng)建一個(gè)開始form標(biāo)簽,相對于你的配置文檔中的基礎(chǔ)URL。
允許你添加一些form屬性和一些隱藏表單,并且他會(huì)基于你的 config.php 文件里設(shè)置的編碼,自動(dòng)生成 accept-charset 這個(gè)屬性。使用這個(gè)函數(shù)而不是直接硬編碼HTML的主要的優(yōu)勢是使你的程序可以方便的轉(zhuǎn)換,如果你的URL變化的話。
echo form_open('email/send');上面的例子會(huì)創(chuàng)建一個(gè)form提交至你的基礎(chǔ)URL加上"email/send" URI片段,像這樣:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" />
添加一些屬性
可以在第二個(gè)參數(shù)里傳遞一個(gè)關(guān)聯(lián)數(shù)組來達(dá)到這一目的, 像這樣:
$attributes = array('class' => 'email', 'id' => 'myform');echo form_open('email/send', $attributes);上面的例子會(huì)創(chuàng)建一個(gè)這樣的form標(biāo)簽:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="myform" />
增加隱藏域
隱藏域可以使用數(shù)組加在第三個(gè)參數(shù)上,就像這樣:
$hidden = array('username' => 'Joe', 'member_id' => '234');echo form_open('email/send', '', $hidden);上面的例子會(huì)創(chuàng)建一個(gè)這樣的form標(biāo)簽和這些隱藏域:
<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()
這個(gè)函數(shù)和上面的form_open()函數(shù)完全一樣,不同之處在于它多了一個(gè)multipart屬性。如果你要制作一個(gè)上傳文件的表單,這個(gè)屬性是必須的。
echo form_hidden('username', 'johndoe');將產(chǎn)生:
<input type="hidden" name="username" value="johndoe" />或者你也可以使用數(shù)組來聯(lián)合創(chuàng)建它們:
$data = array( 'name' => 'John Doe', 'email' => 'john@example.com', 'url' => 'http://example.com' );echo form_hidden($data);將產(chǎn)生:
<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');或者你也可以用關(guān)聯(lián)數(shù)組來添加你想加入的內(nèi)容:
$data = array( 'name' => 'username', 'id' => 'username', 'value' => 'johndoe', 'maxlength' => '100', 'size' => '50', 'style' => 'width:50%', );echo form_input($data);將產(chǎn)生:
<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />如果你想加入一些額外的內(nèi)容,例如Javascript,你可以在第三個(gè)參數(shù)里輸入字符串來創(chuàng)建它:
$js = 'onClick="some_function()"';echo form_input('username', 'johndoe', $js);form_password()
此函數(shù)除了是設(shè)置type為“password”外和上面的 form_input() 函數(shù)完全一樣。
此函數(shù)與上面的 form_input() 函數(shù)幾乎完全相同,唯一的區(qū)別是此函數(shù)所設(shè)置的 type 為 "file",用來上傳文件。
此函數(shù)與上面的 form_input() 函數(shù)幾乎完全相同,唯一的區(qū)別是此函數(shù)所生成的是一個(gè)"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');將會(huì)生成:
<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);將會(huì)生成:
<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> 開始標(biāo)簽包含一些額外的屬性,例如 id 屬性以及JavaScript,你可以將一個(gè)字符串作為第四個(gè)參數(shù)傳遞過去:
$js = 'id="shirts" onChange="some_function();"';echo form_dropdown('shirts', $options, 'large', $js);
如果$options參數(shù)是一個(gè)多維數(shù)組,form_dropdown() 函數(shù)將使用數(shù)組的鍵作為 label值生成一個(gè) <optgroup> 標(biāo)簽。
form_fieldset()
幫助你生成fieldset/legend標(biāo)簽。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>
與其它函數(shù)類似,在第二個(gè)參數(shù)你可以傳遞一個(gè)關(guān)聯(lián)數(shù)組來添加你自己想要的自定義的屬性。
$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);會(huì)生成:
</fieldset></div></div>
echo form_checkbox('newsletter', 'accept', TRUE);將會(huì)生成:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />第三個(gè)參數(shù)為 TRUE/FALSE 布爾值,決定復(fù)選框是否被默認(rèn)選中。
$data = array( 'name' => 'newsletter', 'id' => 'newsletter', 'value' => 'accept', 'checked' => TRUE, 'style' => 'margin:10px', );echo form_checkbox($data);將會(huì)生成:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />與其它函數(shù)相似的,如果你希望此標(biāo)簽包含額外的數(shù)據(jù),例如JavaScript,你可以將一個(gè)字符串作為第四個(gè)參數(shù)傳遞過去:
$js = 'onClick="some_function()"';echo form_checkbox('newsletter', 'accept', TRUE, $js);
form_radio()
此函數(shù)與上面的 form_checkbox() 函數(shù)幾乎完全相同,唯一的區(qū)別是此函數(shù)生成的是 "radio" 單選框。
echo form_submit('mysubmit', 'Submit Post!');會(huì)生成:
<input type="submit" name="mysubmit" value="Submit Post!" />
與其它函數(shù)類似,第一個(gè)參數(shù)你可以傳遞一個(gè)關(guān)聯(lián)數(shù)組來設(shè)置你所需要的屬性。第三個(gè)參數(shù)允許你添加一些額外的內(nèi)容到生成的HTML代碼里。
echo form_label('你的名字是?','username');會(huì)生成:
<label for="username">你的名字是?</label>與其它函數(shù)類似,你可以在第三個(gè)參數(shù)傳入一個(gè)關(guān)聯(lián)數(shù)組來增加一些額外的屬性值。
$attributes = array('class' => 'mycustomclass','style' => 'color: #000;',);echo form_label('你的名字是?', 'username', $attributes);會(huì)生成:
<label for="username" class="mycustomclass" style="color: #000;">你的名字是?</label>
作用是生成一個(gè)標(biāo)準(zhǔn)的 reset 按鈕。此函數(shù)用法與 form_submit() 完全相同。
echo form_button('name','content');將會(huì)生成
<button name="name" type="button">Content</button>或者你也可以將一個(gè)包含有任何你想要的數(shù)據(jù)的關(guān)聯(lián)數(shù)組作為參數(shù)傳遞過去:
$data = array( 'name' => 'button', 'id' => 'button', 'value' => 'true', 'type' => 'reset', 'content' => 'Reset');echo form_button($data);將會(huì)生成:
<button name="button" id="button" value="true" type="reset">Reset</button>如果你希望表單包含一些額外的數(shù)據(jù),例如JavaScript,你可以將一個(gè)字符串作為第三個(gè)參數(shù)傳遞過去:
$js = 'onClick="some_function()"';echo form_button('mybutton', 'Click Me', $js);
$string = "</div></div>";echo form_close($string);將會(huì)生成:
</form></div></div>
$string = 'Here is a string containing "quoted" text.';<input type="text" name="myform" value="<var<$string</var<" />因?yàn)樯厦娴淖址邪艘枺蚨鴮?dǎo)致表單被破壞。form_prep()函數(shù)會(huì)轉(zhuǎn)換HTML,因此可以放心使用:
<input type="text" name="myform" value="<var<<?php echo form_prep($string); ?></var<" />
說明: 如果你使用的是表單輔助函數(shù)中的任何一個(gè),數(shù)據(jù)都會(huì)自動(dòng)的進(jìn)行預(yù)處理,所以沒有必要調(diào)用本函數(shù)。只有當(dāng)你手動(dòng)創(chuàng)建表單元素時(shí),你才需要本函數(shù)。
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
當(dāng)上面的表單元素第一次加載時(shí)將會(huì)顯示"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()
允許你顯示一個(gè)處于提交狀態(tài)的復(fù)選框。第一個(gè)參數(shù)必須包含此復(fù)選框的名稱,第二個(gè)參數(shù)必須包含它的值,第三個(gè)參數(shù)是可選的,作用是設(shè)置復(fù)選框?yàn)槟J(rèn)選中狀態(tài)(使用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'); ?> />
允許你顯示那些處于提交狀態(tài)的單選框。這個(gè)函數(shù)與前面的 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'); ?> />