2015-02-23 67 views
0

我完全是Drupal Ajax的新手。在我的项目中,只有提交按钮的简单表单用于向数据库插入“是”值。所以应该用Drupal Ajax来完成。所以当该按钮提交的值应该存储在Db与页面加载和提交后显示文本“成功选择”,而不是按钮。使用ajax插入查询drupal

所以请任何人都可以帮我完成这个任务

回答

0

在这里,我创建了一个按钮,提交时没有页面重新加载。

<?php 

function db_store_form($form, &$form_state) 
{ 
    $form['add_button'] = array(
     '#type' => 'submit', 
     '#name' => 'Yes', 
     '#value' => 'Yes', 
     '#prefix' => '<div id="wrapper">', 
     '#suffix' => '</div>', 
     '#ajax' => array(
      'callback' => 'ajax_yes_callback', 
      'wrapper' => 'wrapper', 
      'method' => 'replace', 
      'effect' => 'fade', 
     ), 
    ); 
    return $form; 
} 

function db_store_form_submit($form, &$form_state) 
{ 
    drupal_set_message('Yes has been stored in yes_variable'); 
    // Store yes in db 
    variable_set('yes_variable', 'yes'); 
} 

function ajax_yes_callback($form, $form_state) { 
    return $form['add_button']; 
} 

function db_store_menu() 
{ 
    $items = array(); 

    $items['db_store'] = array(
     'title'   => 'db store', 
     'page callback' => 'drupal_get_form', 
     'page arguments' => array('db_store_form'), 
     'access callback' => array(TRUE), 
     'type'   => MENU_CALLBACK, 
    ); 

    return $items; 
}