表单辅助函数
The Form Helper file contains functions that assist in working with forms.
载入辅助函数
用下面的代码载入该辅助函数:
$this->load->helper('form');
The following functions are available:
form_open()
创建一个开始form标签,相对于你的配置文档中的基础URL。允许你添加一些form属性和一些隐藏表单。
使用这个函数而不是直接硬编码HTML的主要的优势是使你的程序可以方便的转换,如果你的URL变化的话。
下面是一个例子:
echo form_open('email/send');
上面的例子会创建一个form提交至你的基础URL加上"email/send" URI片段,像这样:
<form method="post" action="http://example.com/index.php/email/send" />
添加一些属性
可以在第二个参数里传递一个关联数组来达到这一目的, 像这样:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
上面的例子会创建一个这样的form标签:
<form method="post" 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" 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()
原文:This function is absolutely identical to the form_open() tag above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.
试译文:这个函数除了增加了多属性其余的和上面的form_open()标签一样,当你使用这个表单上传文件时,需要使用到它。
form_hidden()
可以使你创建一个隐藏输入栏。你可以输入name和value来创建一个:
form_hidden('username', 'johndoe');
// 将产生:
<input type="hidden" name="username" value="johnodoe" />
或者你也可以使用数组来联合创建它们:
$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" />
form_input()
可以使你创建一个标准输入栏。你可以在第一和第二个参数里输入name和value来创建一个:
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()
This function is identical in all respects to the form_input() function above except that is sets it as a "password" type.
form_upload()
This function is identical in all respects to the form_input() function above except that is sets it as a "file" type, allowing it to be used to upload files.
form_textarea()
This function is identical in all respects to the form_input() function above except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above example, you will instead specify "rows" and "cols".
form_dropdown()
Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array through the third parameter, and CodeIgniter will create a multiple select for you. Example:
$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');
// Would produce:
<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);
// Would produce:
<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>
If you would like the opening <select> to contain additional data, like JavaScript, you can pass it as a string in the fourth parameter:
$js = 'onChange="some_function()"';
echo form_dropdown('shirts', $options, 'large', $js);
form_fieldset()
帮助你生成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>
与其它函数类似,在第二个参数你可以传递一个关联数组来添加你自己想要的自定义的属性。
$attributes = array('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()
生成一个结束的fieldset标签。唯一的优势是,使用该函数,你可以传递数据,这些数据会附加在这个标签的后面。例如:
$string = "</div></div>";
echo fieldset_close($string);
// 会生成:
</fieldset>
</div></div>
form_checkbox()
Lets you generate a checkbox field. Simple example:
echo form_checkbox('newsletter', 'accept', TRUE);
// Would produce:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />
The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.
Similar to the other form functions in this helper, you can also pass an array of attributes to the function:
$data = array(
'name' => 'newsletter',
'id' => 'newsletter',
'value' => 'accept',
'checked' => TRUE,
'style' => 'margin:10px',
);
echo form_checkbox($data);
// Would produce:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />
As with other functions, if you would like the tag to contain additional data, like JavaScript, you can pass it as a string in the fourth parameter:
$js = 'onClick="some_function()"';
echo form_checkbox('newsletter', 'accept', TRUE, $js)
form_radio()
This function is identical in all respects to the form_checkbox() function above except that is sets it as a "radio" type.
form_submit()
帮助你生成一个标准的submit按钮。示例如下:
echo form_submit('mysubmit', 'Submit Post!');
// 会生成:
<input type="submit" name="mysubmit" value="Submit Post!" />
与其它函数类似,第一个参数你可以传递一个关联数组来设置你所需要的属性。第三个参数允许你添加一些额外的内容到生成的HTML代码里。
form_label()
帮助你生成一个label.示例如下:
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>
form_reset()
Lets you generate a standard reset button. Use is identical to form_submit().
form_button()
Lets you generate a standard button element. You can minimally pass the button name and content in the first and second parameter:
echo form_button('name','content');
// Would produce
<button name="name" type="submit">Content</button>
Or you can pass an associative array containing any data you wish your form to contain:
$data = array(
'name' => 'button',
'id' => 'button',
'value' => 'true',
'type' => 'reset',
'content' => 'Reset'
);
echo form_button($data);
// Would produce:
<button name="button" id="button" value="true" type="reset">Reset</button>
If you would like your form to contain some additional data, like JavaScript, you can pass it as a string in the third parameter:
$js = 'onClick="some_function()"';
echo form_button('mybutton', 'Click Me', $js);
form_close()
Produces a closing </form> tag. The only advantage to using this function is it permits you to pass data to it which will be added below the tag. For example:
$string = "</div></div>";
echo form_close($string);
// Would produce:
</form>
</div></div>
form_prep()
Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:
$string = 'Here is a string containing "quoted" text.';
<input type="text" name="myform" value="<var<$string</var<" />
Since the above string contains a set of quotes it will cause the form to break. The form_prep function converts HTML so that it can be used safely:
<input type="text" name="myform" value="<var<<?php echo form_prep($string); ?></var<" />
Note: If you use any of the form helper functions listed in this page the form values will be prepped automatically, so there is no need to call this function. Use it only if you are creating your own form elements.