- Drupal
- Cài đặt và sử dụng
- Sử dụng các module cộng thêm
- Drupal 6.x
- Mánh và mẹo
- Phát triển Drupal module
- Sưu tập các bài viết hay
- Sử dụng SSH comand để quản lý Drupal site
- Theming
- Thủ thuật
Như chúng ta đã biết, nói đến ứng dụng web là phải nói đến form. Người lập trình web rất thường xuyên gặp lỗi bảo mật khi sử lý form nhập liệu. Công việc xử lý form nhiều lúc cũng rất nhiêu khê.
Drupal là một PHP web framework, do đó chắc chắn là phải hỗ trợ việc xử lý form. FormAPI mà Drupal cung cấp vô cùng mạnh mẽ, cung cấp cách thức xây dựng form dễ dàng, kiểm tra tính hợp lệ của dữ liệu cũng đơn giản, tùy biến giao diện tự do.
Bằng formAPI, các phần tử của form được định nghĩa bằng các mảng với các khóa tuân theo chuẩn (Drupal). Chúng ta tạo một cái form xem nào:
<?php
// tạo một form
$form['foo'] = array(
'#type' => 'textfield',
'#title' => t('bar'),
'#default_value' => $edit['foo'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('baz'),
);
?><?php
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
?>Một số điểm cần chú ý:
$form. Thí dụ, nếu một phần tử của form được định nghĩa dạng:<?php
$form['account_settings']['username']
?>$_POST['edit']['user'] (hoặc $form_values)
['#type']
'#value' cho những phần tử form có thể thay đổi bởi người dùng. Nếu cần thì nên dùng khóa thuộc tính '#default_value'.
Bây giờ chúng ta sẽ xem xét một ví dụ sử dụng FormAPI cụ thể hơn:
<?php
function test_page() {
// Access log settings:
$options = array(
'1' => t('Enabled'),
'0' => t('Disabled')
);
$form['access'] = array(
'#type' => 'fieldset',
'#title' => t('Access log settings'),
'#tree' => TRUE,
);
$form['access']['log'] = array
(
'#type' => 'radios',
'#title' => t('Log'),
'#default_value' => variable_get('log', 0),
'#options' => $options,
'#description' => t('The log.'),
);
$period = drupal_map_assoc(
array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800),
'format_interval'
);
$form['access']['timer'] = array(
'#type' => 'select',
'#title' => t('Discard logs older than'),
'#default_value' => variable_get('timer', 259200),
'#options' => $period,
'#description' => t('The timer.'),
);
// Description
$form['details'] = array(
'#type' => 'fieldset',
'#title' => t('Details'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['details']['description'] = array(
'#type' => 'textarea',
'#title' => t('Describe it'),
'#default_value' => variable_get('description', ''),
'#cols' => 60,
'#rows' => 5,
'#description' => t('Log description.'),
);
$form['details']['admin'] = array(
'#type' => 'checkbox',
'#title' => t('Only admin can view'),
'#default_value' => variable_get('admin', 0),
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 30,
'#maxlength' => 64,
'#description' => t('Enter the name for this group of settings'),
);
$form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
$output = drupal_get_form('test_page', $form);
return $output;
}
?>------------------------------------------------------------------
Xem thêm:
[1] Thử nhanh php code nhanh bằng Drupal
Comments
Unsupported operand types in
Unsupported operand types in C:\wamp\www\drupal-5.2\includes\form.inc on line 341
bao loi nay minh khong biet lam sao. Minh moi bat dau xem drupal thoi.
Cuu em voi
EmGaiVuiVe
Bạn có thể gửi script
Bạn có thể gửi script của bạn lên đây được không?
Post new comment