Connecting to database and inserting using ajax form
First question that strikes our mind is why we should insert using ajax
form. Through ajax web pages will be updated without
page reloading of the entire page. So it is good idea to insert using ajax
forms. Database setting is stored in the folder sites/default/settings.php
file, where we can find the database information. While installing the drupal site,
database is also created. Now we have to connect to the database and insert the
ajax form into it and display the
values. To do so first we have to create the ajax form
by implementing the hook_menu. In this example there is one field in the
form named Full Name. On pressing the Submit button it will save the data into testdb
table through Ajax.
Creating ajax form
In the above code
$items['testdb'] = array( => URL as " testdb" will be created.
title => It is the page title.
page callback => It is the function called when page is accessed.
access argument => It is the key for an array which has permission for the current user to access the page.
type => MENU_NORMAL_ITEM => It is the normal menu item which is shown in menu and breadcrumbs.
Now we have to create textfield "Full Name" and submit it. To do so following is the code:
#ajax => This form
element will trigger a background Ajax call when it is changed or clicked.
#ajax['callback'] => This is simple function which selects and return the portion of the form that is to be replaced on the original page. Here the callback function is contact_us_send_message
Callback function:
After clicking on submit the value will be stored in the database, where the
callback function
contact_us_send_message is called and returns $name
Creating ajax form
function contact_us_menu()
{
$items = array();
$items['testdb'] = array
(
'title' => 'Contact Us',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_us_form'),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
In the above code
$items['testdb'] = array( => URL as " testdb" will be created.
title => It is the page title.
page callback => It is the function called when page is accessed.
access argument => It is the key for an array which has permission for the current user to access the page.
type => MENU_NORMAL_ITEM => It is the normal menu item which is shown in menu and breadcrumbs.
Now we have to create textfield "Full Name" and submit it. To do so following is the code:
function contact_us_form($form, &$form_state)
{
$form = array();
$form['ajax_markup'] = array
(
'#type' => 'markup',
'#prefix' => '<div id="show_ajax_message_div">',
'#suffix' => '</div>',
'#markup' => '',
);
$form['name'] = array
(
'#type' => 'textfield',
'#title' => 'Full Name',
'#size' => 40,
'#maxlength' =>120,
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
'#ajax' => array
(
'callback' => 'contact_us_send_message',
'wrapper' => 'show_ajax_message_div',
'method' => 'html',
'effect' => 'fade',
),
);
return $form;
}
#ajax['callback'] => This is simple function which selects and return the portion of the form that is to be replaced on the original page. Here the callback function is contact_us_send_message
Callback function:
function contact_us_send_message($form, &$form_state)
{
$name = $form_state['values']['name'];
$result = db_insert('firstapp') ->fields(array(
'name' => $form_state['values']['name'],
))->execute();
return $name;
}
contact_us_send_message is called and returns $name
1Fully practical and project based
sessions from first class.
2Training by Experienced
Consultants, not regular Trainers
3Friendly and enthusiastic faculty
to clear your doubts 24X7
4Free Live project after the
training to get you industry experience
If you want more
details please visit us:
Zenrays.com Reach us at
trainings@zenrays.com
ABOUT THE AUTHOR
Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible
0 comments:
Post a Comment