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:

  1. 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)
  2. Kieu cua phan tu form duoc dinh nghia o khoa ['#type']
  3. Cac khoa thuoc tinh cua phan tu duoc dinh nghia dang mot chuoi, bat dau bang dau sharp (#).
  4. 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.
  5. 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(
360010800216003240043200864001728002592006048001209600241920048384009676800),
    
'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?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <br> <p> <h3>
  • Lines and paragraphs break automatically.
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.