CodeIgnitor: Form Helper - Phần 2


Khóa học qua video:
Python C# Lập trình C Java SQL Server PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

form_upload($data,$value,$extra)

Tham số:
  • $data (array) – Các thuộc tính của thẻ
  • $value (string) – Giá trị của thẻ
  • $extra (mixed) – Các thuộc tính mở rộng của thẻ
Trả về:

Thẻ <input type="file">

Kiểu trả về: 

string

form_textarea($data,$value,$extra)

Tham số:
  • $data (array) – Các thuộc tính của thẻ
  • $value (string) – Giá trị của thẻ
  • $extra (mixed) – Các thuộc tính mở rộng của thẻ
Trả về:

Thẻ <textarea>

Kiểu trả về: 

string

Lưu ý
Cần thay thế các thuộc tính maxlength và size bằng các thuộc tính rows và cols.

form_dropdown($name,$options,$selected,$extra)

Tham số:
  • $name (string) – Tên của thẻ <select>
  • $options (array) – Mảng chứa các thẻ <option>
  • $selected (array) – Các <option> được đánh dấu với thuộc tính selected
  • $extra (mixed) – Các thuộc tính mở rộng
Trả về:

Thẻ <select> và các thẻ con <option>

Kiểu trả về: 

string

Hàm này dùng để tạo một thẻ <select> với các thẻ con <option>. Bạn cũng có thể truyền một mảng để tạo nhiều thẻ <select>.

Ví dụ:

$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');

/*
 Sẽ tạo ra:

 <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);

/*
 Sẽ tạo ra:

 <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>
*/

Bạn cũng có thể truyền JavaScript bằng cách như sau:

$js = 'id="shirts" onChange="some_function();"';
echo form_dropdown('shirts', $options, 'large', $js);

Hoặc truyền ở dạng mảng:

$js = array(
        'id'       => 'shirts',
        'onChange' => 'some_function();'
);
echo form_dropdown('shirts', $options, 'large', $js);

Nếu mảng truyền $options là mảng đa chiều thì form_dropdown() sẽ tạo một <optgroup> với key của mảng là các nhãn.

form_multiselect($name,$options,$selected,$extra)

Tham số:
  • $name (string) – Tên của thẻ <select>
  • $options (array) – Mảng chứa các thẻ <option>
  • $selected (array) – Các <option> được đánh dấu với thuộc tính selected
  • $extra (mixed) – Các thuộc tính mở rộng
Trả về:

Thẻ <select> và các thẻ con <option> với thuộc tính multiple kèm theo (tạo một dropdown multiselect)

Kiểu trả về: 

string

Hàm này tương tự hàm form_dropdown() nhưng có thêm thuộc tính multiple để tạo danh sách cho phép chọn nhiều tùy chọn

form_fieldset($legend_text,$attributes)

Tham số:
  • $legend_text (string) – Chuỗi đặt trong thẻ <legend>
  • $attributes (array) – Các thuộc tính của thẻ <fieldset>
Trả về:

Thẻ <fieldset> và thẻ con <legend>

Kiểu trả về:

string

Ví dụ:

echo form_fieldset('Address Information');
echo "<p>fieldset content here</p>\n";
echo form_fieldset_close();

/*
 Sẽ tạo ra:

 <fieldset>
  <legend>Address Information</legend>
   <p>form content here</p>
 </fieldset>
*/

Tương tự như các hàm khác, bạn có thể submit một mảng ở tham số thứ hai nếu bạn muốn thêm các thuộc tính, ví dụ:

$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();

/*
 Sẽ tạo ra:

 <fieldset id="address_info" class="address_info">
  <legend>Address Information</legend>
   <p>form content here</p>
 </fieldset>
*/

form_fieldset_close($extra)

Tham số:

$extra (string) – Thêm vào sau thẻ đóng </fieldset>

Trả về:

Thẻ đóng </fieldset>

Kiểu trả về: 

string

Ví dụ:

$string = '</div></div>';
echo form_fieldset_close($string);
// Sẽ tạo ra: </fieldset></div></div>

form_checkbox($data,$value,$checked,$extra)

Tham số:
  • $data (array) – Các thuộc tính của thẻ <checkbox>
  • $value (string) – Giá trị của thẻ
  • $checked (bool) – Tích mặc định cho thẻ hay không
  • $extra (mixed) – Các thuộc tính mở rộng của thẻ
Returns:

An HTML checkbox input tag

Return type:

string

Hàm này dùng để tạo thẻ <input type="checkbox">. Ví dụ:

echo form_checkbox('newsletter', 'accept', TRUE);
// Sẽ tạo ra:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />

Tham số thứ ba của hàm dùng để xác định có mặc định tích trước cho checkbox hay không (thông qua thuộc tính checked).

Tương tự như những hàm khác thì ta cũng có thể thiết lập các thuộc tính cho checkbox bằng cách truyền mảng thuộc tính tới hàm. Ví dụ:

$data = array(
        'name'          => 'newsletter',
        'id'            => 'newsletter',
        'value'         => 'accept',
        'checked'       => TRUE,
        'style'         => 'margin:10px'
);

echo form_checkbox($data);
// Sẽ tạo ra:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />

Nếu bạn cũng muốn thêm code như JavaScript chẳng hạn cho checkbox thì bạn có thể làm như sau đối với thuộc tính thứ tư của hàm:

$js = 'onClick="some_function()"';
echo form_checkbox('newsletter', 'accept', TRUE, $js);

Hoặc bạn có thể truyền nó như một mảng:

$js = array('onClick' => 'some_function();');
echo form_checkbox('newsletter', 'accept', TRUE, $js);

form_radio($data,$value,$checked,$extra)

Tham số:
  • $data (array) – Các thuộc tính của <radio>
  • $value (string) – Giá trị của radio
  • $checked (bool) – Tích hay không tích cho radio
  • $extra (mixed) – Các thuộc tính mở rộng
Trả về:

Thẻ <input type="radio">

Kiểu trả về:

string

Hàm này có tác dụng tương tự như hàm form_checkbox(), chỉ khác là nó tạo ra thẻ <input type="radio">.

form_label($label_text,$id,$attributes)

Tham số:
  • $label_text (string) – Nhãn cho thẻ <label>
  • $id (string) – ID của phần tử form
  • $attributes (string) – Các thuộc tính của <label>
Trả về: Thẻ <label>
Kiểu trả về:

string

Hàm này dùng để tạo thẻ <label>. Ví dụ:

echo form_label('What is your Name', 'username');
// Sẽ tạo ra:
<label for="username">What is your Name</label>

Ví dụ sau sẽ tạo thẻ label với các thuộc tính thêm vào:

$attributes = array(
        'class' => 'mycustomclass',
        'style' => 'color: #000;'
);

echo form_label('What is your Name', 'username', $attributes);
// Sẽ tạo ra:
<label for="username" class="mycustomclass" style="color: #000;">What is your Name</label>

form_submit($data,$value,$extra)

Tham số:
  • $data (string) – Tên của nút submit
  • $value (string) – Giá trị của nút submit
  • $extra (mixed) – Các thuộc tính mở rộng cho submit
Trả về: Thẻ <input type="submit">
Kiểu trả về:

string

Hàm này dùng để tạo nút lệnh submit. Ví dụ:

echo form_submit('mysubmit', 'Submit Post!');
// Sẽ tạo ra:
<input type="submit" name="mysubmit" value="Submit Post!" />

Tương tự như các hàm khác, ta có thể thiết lập các thuộc tính cho submit thông qua mảng các thuộc tính ở tham số thứ ba của hàm.

form_reset($data,$value,$extra)

Tham số:
  • $data (string) – Tên của nút reset
  • $value (string) – Giá trị của reset
  • $extra (mixed) – Các thuộc tính mở rộng
Trả về:

Thẻ <input type="reset">

Kiểu trả về: 

string

Hàm này dùng để tạo nút lệnh reset (<input type="reset">). Hàm này thường dùng với hàm form_submit().

» Tiếp: Form Helper - Phần 3
« Trước: Form Helper - Phần 1
Khóa học qua video:
Python C# Lập trình C Java SQL Server PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!