文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在drupal中使用“confirm_form()”进行确认动作

在drupal中使用“confirm_form()”进行确认动作

时间:2009-07-17  来源:AppleDragon

效果如图:

500)this.width=500;" border="0">
点delete 后

500)this.width=500;" border="0">

We can easily create an action confirm form using confirm_form() function in Drupal. Normally we need this form when user want to delete something and we want user to confirm his/her action. I am showing here an example in which we are listing all the Doctors information with Edit and Delete option. When user click the delete link we will show him the action confirm form with “Delete” and “Cancel” options. If user click the “Delete” button we will delete that Doctor record from database and if user click on “Cancel” we simply show the doctors list again.

Here is the code. I am using the module name “doctor” in this example.

Step 1. First implement hook_menu() to register new path in Drupal.



function doctor_menu() {
  $items = array();
 
  $items['doctors'] = array(
    'title' => t('Doctors'),
    'page callback' => 'doctors_list',
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  $items['doctors/delete/%doctor_user'] = array(
    'title' => t('Delete doctor'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doctor_delete_confirm', 2),
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  return $items;
}



Step 2. Implementing the doctors_list() function to display the list of doctors.

function doctors_list() {
    $header = array(t('Doctor Name'), t('Gender'), t('Phone No.'), t('Status'), t('Action'));
 
    $query = "SELECT * FROM {doctor}";
    $rs = db_query($query);
 
    $row = array();
 
    if ($rs) {
        while ($data = db_fetch_object($rs)) {
            $gender = $data->gender == 'M' ? t('Male') : t('Female');
            $status = $doctor->status ? t('active') : t('inactive');
            $row[] = array(stripslashes($data->firstname) . ' ' . stripslashes($data->lastname), $gender, stripslashes($data->phoneno), $status,
            "<a href='/doctors/edit/{$data->doctorid}'>" . t('Edit') . "</a> | <a href='/doctors/delete/{$data->doctorid}'>" . t('Delete') . "</a>");
        }
    }
 
    $str .= theme_table($header, $row);
 
    return $str;
}



Step 3. Create a wildcard loader function doctor_user_load() which we used in “doctors/delete/%doctor_user” path. This function checks the given “ID” in path is valid or not. If the given doctor id is not exists in database then “Page not found.” message will display.

function doctor_user_load($doctorid) {
    $query = "SELECT * FROM {doctor} WHERE doctorid = %d";
    $rs = db_query($query, $doctorid);
 
    if ($rs) {
        while ($data = db_fetch_object($rs)) {
            return $data;
        }
    }
 
    return FALSE;
}


Step 4. Create the doctor_delete_confirm() function. We are using the “confirm_form()” function in this function to show action confirm form.

function doctor_delete_confirm(&$form_state, $doctor) {
    $form['_doctor'] = array(
        '#type' => 'value',
        '#value' => $doctor,
    );
 
    return confirm_form($form,
        t('Are you sure you want to delete this doctor?'),
        isset($_GET['destination']) ? $_GET['destination'] : "doctors",
        t('This action cannot be undone.'),
        t('Delete'),
        t('Cancel'));
}


Step 5. Create the Form API submit function for doctor_delete_confirm form.

function doctor_delete_confirm_submit($form, &$form_state) {
    $form_values = $form_state['values'];
 
    if ($form_state['values']['confirm']) {
        $doctor = $form_state['values']['_doctor'];
 
        doctor_delete($form_state['values'], $doctor->doctorid);            
 
        drupal_set_message(t('Doctor has been deleted successfully.'));
      }
 
    drupal_goto("doctors");
}

排行榜 更多 +
边境检察最后区域手机版下载

边境检察最后区域手机版下载

角色扮演 下载
酋长你别跑手游下载

酋长你别跑手游下载

休闲益智 下载
心动漫画app下载官方版

心动漫画app下载官方版

浏览阅读 下载