表單輔助函數(shù)?
表單輔助函數(shù)包含的函數(shù)輔助表單運行.
換碼(轉(zhuǎn)義)字段值?
你也許需要使用 HTML 和字符像在你的表單內(nèi)部的元素里引用。為了安全地執(zhí)行,你將需要使用:doc:common function <../general/common_functions>
esc()
.
考慮下文的示例:
$string = 'Here is a string containing "quoted" text.';
<input type="text" name="myfield" value="<?= $string; ?>" />
由于上面字符串包含一套引用,那將導(dǎo)致表單中斷。
The esc()
函數(shù)轉(zhuǎn)換 HTML 特殊字節(jié)以便它能安全地使用:
<input type="text" name="myfield" value="<?= esc($string); ?>" />
注解
如果你在頁面使用任意表單輔助函數(shù)列舉,并且你傳達(dá)像組合的數(shù)組一樣的值,表單值將會被自動換碼,所以不需要調(diào)用這個函數(shù)。使用它只有你要創(chuàng)建你自己的將要傳達(dá)作為字符串的表單元素。
通用函數(shù)?
接下來的函數(shù)是通用的:
-
form_open
([$action = ''[, $attributes = ''[, $hidden = array()]]])? 參數(shù): - $action (string) – 表單行為/目標(biāo) URI 字符串
- $attributes (mixed) – HTML 屬性,就像數(shù)組或者換碼字符串
- $hidden (array) – 隱藏字段的定義的一組數(shù)組An array of hidden fields’ definitions
返回: HTML 表單隨時可用的 tag
返回類型: string
創(chuàng)建一個帶著基地址URL的隨時可用的表單標(biāo)簽**從你的配置優(yōu)先選擇營造**. 它將隨意地讓你添加表單屬性和隱藏輸入字段,并且會常常在你的配置文件里添加基于 charset 值的 accept-charset 屬性。
寧可使用標(biāo)簽的絕對好處也不要艱苦的編碼你自己的 HTML 是由于在事件里你的 URLs 曾改變而標(biāo)簽容許你的網(wǎng)址是更便攜的。
下面是一則簡單的例子:
echo form_open('email/send');
上面的例子將創(chuàng)建一個指向你的基地址 URL 和 “email/send” URL 部分的表單,像這樣:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send">
You can also add {locale} like the following:
echo form_open('{locale}/email/send');
The above example would create a form that points to your base URL plus the current request locale with “email/send” URI segments, like this:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/en/email/send">
添加屬性
由正傳達(dá)組合的數(shù)組到第二個參數(shù)的屬性能被加入,像這樣:
$attributes = ['class' => 'email', 'id' => 'myform']; echo form_open('email/send', $attributes);
二選一地,你能明確的像字符串一樣說明第二個參數(shù):
echo form_open('email/send', 'class="email" id="myform"');
上文的例子將會創(chuàng)建一個同樣的表單相似于下文這個事例:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="myform">
If CSRF filter is turned on form_open() will generate CSRF field at the beginning of the form. You can specify ID of this field by passing csrf_id as one of the $attribute array:
form_open(‘/u/sign-up’, [‘csrf_id’ => ‘my-id’]);will return:
<form action=”/u/sign-up” method=”post” accept-charset=”utf-8”> <input type=”hidden” id=”my-id” name=”csrf_field” value=”964ede6e0ae8a680f7b8eab69136717d” />添加隱藏輸入字段
由正傳達(dá)組合的數(shù)組到第三個參數(shù)的隱藏字段能被添加,像這樣:
$hidden = ['username' => 'Joe', 'member_id' => '234']; echo form_open('email/send', '', $hidden);
由正傳達(dá)的任何false值到隱藏字段,你能忽略第二個參數(shù).
上面的事例將創(chuàng)建類似于下面的句子:
<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
([$action = ''[, $attributes = ''[, $hidden = array()]]])? 參數(shù): - $action (string) – 表單行為/目標(biāo) URI 字符串
- $attributes (mixed) – HTML 屬性,就像數(shù)組或者換碼字符串
- $hidden (array) – 隱藏字段的定義的一組數(shù)組
返回: HTML 多部件的表單隨時可用的 tag
返回類型: string
這個函數(shù)對上文的
form_open()
來說是類似的, 除了它附加了一個 multipart 屬性,如果你喜歡使用表單上傳文件這個屬性是必須的。
參數(shù): - $name (string) – 字段名
- $value (string) – 字段值
返回: HTML 隱藏輸入字段 tag
返回類型: string
讓你生成隱藏輸入字段。你也能提交名稱/值字符串去創(chuàng)建一個字段:
form_hidden('username', 'johndoe'); // 將產(chǎn)生: <input type="hidden" name="username" value="johndoe" />
… 或者你能提交組合數(shù)組去創(chuàng)建復(fù)合字段:
$data = [ '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" /> */
你也能傳達(dá)組合的數(shù)組給字段值:
$data = [ 'name' => 'John Doe', 'email' => 'john@example.com', 'url' => 'http://example.com' ]; echo form_hidden('my_array', $data); /* 將產(chǎn)生: <input type="hidden" name="my_array[name]" value="John Doe" /> <input type="hidden" name="my_array[email]" value="john@example.com" /> <input type="hidden" name="my_array[url]" value="http://example.com" /> */
倘若你想創(chuàng)建額外屬性的隱藏輸入字段:
$data = [ 'type' => 'hidden', 'name' => 'email', 'id' => 'hiddenemail', 'value' => 'john@example.com', 'class' => 'hiddenemail' ]; echo form_input($data); /* 將產(chǎn)生: <input type="hidden" name="email" value="john@example.com" id="hiddenemail" class="hiddenemail" /> */
-
form_input
([$data = ''[, $value = ''[, $extra = ''[, $type = 'text']]]])? 參數(shù): - $data (array) – 字段屬性數(shù)據(jù)
- $value (string) – 字段值
- $extra (mixed) – 額外屬性被添加到 tag 任何一方像數(shù)組或者文字字符串
- $type (string) – 輸入字段類型。例如: ‘text’, ‘email’, ‘number’, 等等.
返回: HTML 文本輸入字段 tag
返回類型: string
讓你生成標(biāo)準(zhǔn)的文本輸入字段。你能最低程度地在第一和第二參數(shù)里傳達(dá)字段名和值:
echo form_input('username', 'johndoe');
或者你能傳達(dá)包含你希望你的表單要包含的任何數(shù)據(jù)的組合的數(shù)組:
$data = [ 'name' => 'username', 'id' => 'username', 'value' => 'johndoe', 'maxlength' => '100', 'size' => '50', 'style' => 'width:50%' ]; echo form_input($data); /* 將產(chǎn)生: <input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" /> */
如果你想要你的表單包含一些額外的數(shù)據(jù),像 JavaScript ,你能在第三參數(shù)里像字符串一樣傳達(dá)參數(shù):
$js = 'onClick="some_function()"'; echo form_input('username', 'johndoe', $js);
或者你能像數(shù)組一樣傳達(dá)參數(shù):
$js = ['onClick' => 'some_function();']; echo form_input('username', 'johndoe', $js); 支持HTML5 輸入字段擴(kuò)充范圍,你能像第四個參數(shù)一樣傳達(dá)一個輸入鍵入信息:: echo form_input('email', 'joe@example.com', ['placeholder' => 'Email Address...'], 'email'); /* 將產(chǎn)生: <input type="email" name="email" value="joe@example.com" placeholder="Email Address..." /> */
-
form_password
([$data = ''[, $value = ''[, $extra = '']]])? 參數(shù): - $data (array) – 字段屬性數(shù)據(jù)
- $value (string) – 字段值
- $extra (mixed) – 額外的屬性被添加到tag任何一方像數(shù)組或者文字的字符串
返回: HTML 密碼輸入字段 tag
返回類型: string
此函數(shù)除了函數(shù)使用的 “password” 輸入類型在完全關(guān)系到上文所述的
form_input()
函數(shù)是完全相似的。
-
form_upload
([$data = ''[, $value = ''[, $extra = '']]])? :param array $data:字段屬性數(shù)據(jù) :param string $value:字段值 :param mixed $extra: 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串 :returns: HTML 文件上傳輸入字段 tag :rtype: string
此函數(shù)除了使用 “file” 輸入類型在完全關(guān)系到上文所述的
form_input()
函數(shù)是完全相似的,接受函數(shù)適用于上傳文件。
-
form_textarea
([$data = ''[, $value = ''[, $extra = '']]])? 參數(shù): - $data (array) – 字段屬性數(shù)據(jù)
- $value (string) – 字段值
- $extra (mixed) – 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串
返回: HTML 文本區(qū)域 tag
返回類型: string
此函數(shù)除了產(chǎn)生 “textarea” 類型外在完全關(guān)系到上文所述的
form_input()
函數(shù)是完全相似的。注解
上文的例子里代替 maxlength 和 size 屬性,你會更換具體指定的 rows 和 cols 。
-
form_dropdown
([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])? 參數(shù): - $name (string) – 字段名
- $options (array) – 選項的組合的數(shù)組被列舉
- $selected (array) – 字段的列表要標(biāo)明 selected 屬性
- $extra (mixed) – 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串
返回: HTML 下拉菜單選擇字段 tag
返回類型: string
讓你創(chuàng)建一個下拉菜單字段。第一個參數(shù)會包含字段名,第二個參數(shù)會包含一個組合的數(shù)組選項,而第三參數(shù)會包含你希望被選擇的值。你也能通過第三參數(shù)傳達(dá)一個符合選項數(shù)組,并且輔助函數(shù)會為你創(chuàng)建一個復(fù)合選項。
例如:
$options = [ 'small' => 'Small Shirt', 'med' => 'Medium Shirt', 'large' => 'Large Shirt', 'xlarge' => 'Extra Large Shirt', ]; $shirts_on_sale = ['small', 'large']; echo form_dropdown('shirts', $options, 'large'); /* 將產(chǎn)生: <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); /* 將產(chǎn)生: <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> 包含額外的數(shù)據(jù),像 id 屬性或者 JavaScript ,你能在第四個參數(shù)里像字符串一樣傳達(dá)它:: $js = 'id="shirts" onChange="some_function();"'; echo form_dropdown('shirts', $options, 'large', $js);
或者你能像傳達(dá)數(shù)組一樣傳達(dá)參數(shù):
$js = [ 'id' => 'shirts', 'onChange' => 'some_function();' ]; echo form_dropdown('shirts', $options, 'large', $js);
如果數(shù)組被傳達(dá)象
$options
一樣是一個多維數(shù)組,那么form_dropdown()
將會產(chǎn)生一個像 label 一樣帶著數(shù)組鍵碼的 <optgroup> 。
-
form_multiselect
([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])? 參數(shù): - $name (string) – 字段名
- $options (array) – 選項的組合數(shù)組被列舉
- $selected (array) – 字段的列表要標(biāo)明 selected 屬性
- $extra (mixed) – 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串
返回: HTML 下拉菜單混合選項字段 tag
返回類型: string
讓你創(chuàng)建一個標(biāo)準(zhǔn)的混合字段。第一個參數(shù)將包含字段名,第二個參數(shù)會包含選項的一個組合的數(shù)組, 而第三個參數(shù)會包含值或者你想要被選擇的值。
參數(shù)用法是完全相似于上文去使用的
form_dropdown()
,除了當(dāng)然地字段名將需要去用 POST 數(shù)組語法,例如:foo[].
-
form_fieldset
([$legend_text = ''[, $attributes = array()]])? 參數(shù): - $legend_text (string) – Text 放進(jìn) <legend> tag
- $attributes (array) – 屬性被置位在 <fieldset> tag 上
返回: HTML 字段置位開始 tag
返回類型: string
讓你生成 fieldset/legend 字段。
例如:
echo form_fieldset('Address Information'); echo "<p>fieldset content here</p>\n"; echo form_fieldset_close(); /* 生成: <fieldset> <legend>Address Information</legend> <p>form content here</p> </fieldset> */
相似于其他函數(shù),如果你更喜歡設(shè)置額外屬性你能在第二參數(shù)里提交一個組合的數(shù)組:
$attributes = [ 'id' => 'address_info', 'class' => 'address_info' ]; echo form_fieldset('Address Information', $attributes); echo "<p>fieldset content here</p>\n"; echo form_fieldset_close(); /* 生成: <fieldset id="address_info" class="address_info"> <legend>Address Information</legend> <p>form content here</p> </fieldset> */
-
form_fieldset_close
([$extra = ''])? 參數(shù): - $extra (string) – 閉合 tag 附加的任何字段, as is
返回: HTML 字段置位關(guān)閉 tag
返回類型: string
產(chǎn)生一個正關(guān)閉的 </fieldset> tag. 使用這個函數(shù)僅有的優(yōu)勢是它允許你傳達(dá)數(shù)據(jù)給將被添加的下文關(guān)聯(lián)的 tag 。例如
$string = '</div></div>'; echo form_fieldset_close($string); // 將生成: </fieldset></div></div>
-
form_checkbox
([$data = ''[, $value = ''[, $checked = FALSE[, $extra = '']]]])? 參數(shù): - $data (array) – 字段屬性數(shù)據(jù)
- $value (string) – 字段值
- $checked (bool) – 是否去標(biāo)明 checkbox 在 checked 狀態(tài)
- $extra (mixed) – 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串
返回: HTML checkbox 輸入 tag
返回類型: string
讓你產(chǎn)生一個 checkbox 字段. 簡單的例子:
echo form_checkbox('newsletter', 'accept', TRUE); // 將生成: <input type="checkbox" name="newsletter" value="accept" checked="checked" />
第三個參數(shù)包含一個布爾值 TRUE/FALSE 去決定是否 box 應(yīng)該被記號或者未記號。
在這個輔助函數(shù)里類似的對于其他的表單函數(shù)來說,你也能傳達(dá)屬性的數(shù)組給函數(shù):
$data = [ '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" />
也跟其他函數(shù)一樣,如果你想要 tag 去包含像 JavaScript 的額外數(shù)據(jù),你能在第四個參數(shù)里像傳達(dá)字符串一樣傳達(dá)它:
$js = 'onClick="some_function()"'; echo form_checkbox('newsletter', 'accept', TRUE, $js);
或者你能像數(shù)組一樣傳達(dá)它:
$js = ['onClick' => 'some_function();']; echo form_checkbox('newsletter', 'accept', TRUE, $js);
-
form_radio
([$data = ''[, $value = ''[, $checked = FALSE[, $extra = '']]]])? 參數(shù): - $data (array) – 字符串屬性數(shù)據(jù)
- $value (string) – 字符串值
- $checked (bool) – 是否標(biāo)明 radio 按鈕是 checked 狀態(tài)
- $extra (mixed) – 額外的屬性被添加到tag任何一方像數(shù)組或者文字的字符串
返回: HTML radio 輸入 tag
返回類型: string
除了函數(shù)使用 “radio” 輸入類型此函數(shù)在完全關(guān)系到上文所述的
form_checkbox()
函數(shù)是完全類似的。
-
form_label
([$label_text = ''[, $id = ''[, $attributes = array()]]])? 參數(shù): - $label_text (string) – Text 提交 <label> tag
- $id (string) – 我們正在制作的一個 label 表單元素的 ID
- $attributes (string) – HTML 屬性
返回: HTML 字段 label tag
返回類型: string
讓你產(chǎn)生一個 <label>. 簡單事例:
echo form_label('What is your Name', 'username'); // 將生成: <label for="username">What is your Name</label>
相似于其他函數(shù),如果你更喜歡設(shè)置額外的屬性你能在第三個參數(shù)里提交一個組合的數(shù)組.
例如:
$attributes = [ 'class' => 'mycustomclass', 'style' => 'color: #000;' ]; echo form_label('What is your Name', 'username', $attributes); // 將生成: <label for="username" class="mycustomclass" style="color: #000;">What is your Name</label>
-
form_submit
([$data = ''[, $value = ''[, $extra = '']]])? 參數(shù): - $data (string) – Button 名
- $value (string) – Button 值
- $extra (mixed) – 額外的屬性被添加到 tag 任何一方像數(shù)組或者文字的字符串
返回: HTML 輸入submit tag
返回類型: string
讓你產(chǎn)生一個標(biāo)準(zhǔn)的 submit 按鈕。簡單事例:
echo form_submit('mysubmit', 'Submit Post!'); // 將生成: <input type="submit" name="mysubmit" value="Submit Post!" />
相似于其他函數(shù),如果你更喜歡設(shè)置你的本身的屬性你能在第一個參數(shù)里提交一個組合數(shù)組。第三個參數(shù)讓你添加額外的數(shù)據(jù)到你的表單,象 JavaScript.
-
form_reset
([$data = ''[, $value = ''[, $extra = '']]])? 參數(shù): - $data (string) – Button 名
- $value (string) – Button 值
- $extra (mixed) – 額外的屬性被添加到tag任何一方像數(shù)組或者文字的字符串
返回: HTML 輸入重新設(shè)定 button tag
返回類型: string
讓你生成標(biāo)準(zhǔn)重新設(shè)定 button 。 使用習(xí)慣對
form_submit()
是完全相似的.
參數(shù): - $data (string) – Button 名
- $content (string) – Button label
- $extra (mixed) – 額外的屬性被添加到tag任何一方像數(shù)組或者文字的字符串
返回: An HTML button tag
返回類型: string
讓你生成標(biāo)準(zhǔn) button 元素. 你能在第一和第二參數(shù)里最低程度地傳達(dá) button 名稱和內(nèi)容:
echo form_button('name','content'); // 將生成: <button name="name" type="button">Content</button>
或者你能傳達(dá)你的表單去包含你希望包含任何數(shù)據(jù)的一個組合的數(shù)組:
$data = [ '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>
如果你想要你的表單包含一些額外的數(shù)據(jù),例如 JavaScript , 你能在第三個參數(shù)里像字符串一樣傳達(dá)它:
$js = 'onClick="some_function()"'; echo form_button('mybutton', 'Click Me', $js);
-
form_close
([$extra = ''])? 參數(shù): - $extra (string) – 在關(guān)閉 tag 后任何事要追加的, as is
返回: HTML 表單關(guān)閉 tag
返回類型: string
生成正關(guān)閉的 </form> tag. 最佳的優(yōu)勢去使用這個函數(shù)容許你去傳達(dá)數(shù)據(jù)給它,它將會被添加如下文的 tag 。例如:
$string = '</div></div>'; echo form_close($string); // 將生成: </form> </div></div>
-
set_value
($field[, $default = ''[, $html_escape = TRUE]])? 參數(shù): - $field (string) – 字段名
- $default (string) – 默認(rèn)值
- $html_escape (bool) – 是否關(guān)閉 HTML 值的轉(zhuǎn)義
返回: 字段值
返回類型: string
容許你去設(shè)置輸入表單或者文本區(qū)域的值。你必須經(jīng)過函數(shù)的第一個參數(shù)提供字段名。第二個操作參數(shù)允許你為表單設(shè)置一個默認(rèn)值。第三個操作參數(shù)允許你去關(guān)閉 HTML 值的轉(zhuǎn)義,萬一你需要使用此函數(shù)聯(lián)合, 即
form_input()
并規(guī)避雙層轉(zhuǎn)義。例如:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
當(dāng)?shù)谝淮渭虞d時下文的表單將顯示 “0”.
-
set_select
($field[, $value = ''[, $default = FALSE]])? 參數(shù): - $field (string) – 字段名
- $value (string) – 檢測的值
- $default (string) – 是否值也是默認(rèn)的
返回: ‘selected’ 屬性或者一個空字符串
返回類型: string
如果你使用 <select> 菜單, 此函數(shù)允許你顯示已經(jīng)被選擇的菜單題目。.
第一個參數(shù)必須包含選擇菜單的包含名,第二個參數(shù)必須包含選擇菜單包含值, 而第三個操作參數(shù)仍你設(shè)置像默認(rèn)值 (use boolean TRUE/FALSE) 的一個項.
例如:
<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
($field[, $value = ''[, $default = FALSE]])? 參數(shù): - $field (string) – 字段名
- $value (string) – 檢測的值
- $default (string) – 是否值也是默認(rèn)的
返回: ‘checked’ 屬性或者一個空字符串
返回類型: string
容許你在已經(jīng)提交狀況下顯示一個 checkbox.
第一個參數(shù)必須包含 checkbox 的名,第二個參數(shù)必須包含它的值,并且第三個操作參數(shù)讓你設(shè)置一個像默認(rèn)值 (use boolean 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_radio
($field[, $value = ''[, $default = FALSE]])? 參數(shù): - $field (string) – 字段名
- $value (string) – 檢測的值
- $default (string) – 是否值也是默認(rèn)的
返回: ‘checked’ 屬性或者空字符串
返回類型: string
容許你去顯示它們已經(jīng)提交狀態(tài)下的 radio buttons . 此函數(shù)對于上文
set_checkbox()
函數(shù)是完全相似的。事例:
<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'); ?> />
注解
如果你正在使用表單驗證類,你必須常常為你的字段明確說明一個規(guī)范,即使空的,適當(dāng)?shù)臑榱?
set_*()
函數(shù)去工作。 這是因為如果表單驗證對象已經(jīng)定義了,控制器為了set_*()
已經(jīng)送交了類方法替代一般的輔助函數(shù)。