Form API
Dan nhap
Nhu chung ta da biet, noi den ung dung web la phai noi den form. Nguoi lap trinh web rat thuong xuyen gap loi bao mat khi su ly form nhap lieu. Cong viec xu ly form nhieu luc cung rat nhieu khe.
Drupal la mot PHP web framework, do do chac chan la phai ho tro viec xu ly form. FormAPI ma Drupal cung cap vo cung manh me, cung cap cach thuc xay dung form de dang, kiem tra tinh hop le cua du lieu cung don gian, tuy bien giao dien tu do.
Tao form
Bang formAPI, cac phan tu cua form duoc dinh nghia bang cac mang voi cac khoa tuan theo chuan (Drupal). Chung ta tao mot cai form xem nao:
<?php
// tao mot form
$form['foo'] = array(
'#type' => 'textfield',
'#title' => t('bar'),
'#default_value' => $edit['foo'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('baz'),
);
?>va them vao mot nut submit:
<?php
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
?>Mot so diem can chu y:
- Ten thuoc cua mot phan tu duoc dinh nghia trong mang
$form. Thi du, neu mot phan tu cua form duoc dinh nghia dang:
<?php
$form['account_settings']['username']
?>
Thi ten cua thuoc tinh la 'username'. Khi form duoc submit, PHP code co the xu lyd khoa nay thong qua bien$_POST['edit']['user'](hoac$form_values) - Kieu cua phan tu form duoc dinh nghia o khoa
['#type'] - Cac khoa thuoc tinh cua phan tu duoc dinh nghia dang mot chuoi, bat dau bang dau sharp (#).
- Thu tu dinh nghia cac khoa thuoc tinh la khong quan trong. Va mot so thuoc tinh, ban cung khong nhat thiet phai dinh nghia, phan lon chung se duoc gan gia tri mac dinh neu khong duoc dinh nghia.
- Khong su dung khoa thuoc tinh
'#value'cho nhung phan tu form co the thay doi boi nguoi dung. Neu can thi nen dung khoa thuoc tinh'#default_value'.
Bay gio chung ta se xem xet mot vi du su dung FormAPI cu the hon:
<?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 them:
[1] Thu nhanh php code nhanh bang 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
Ban co the gui script
Ban co the gui script cua ban len day duoc khong?
Post new comment