I have a form element below:
//Calls a function that contains the html for templates table.
$form['myid_templates_editor_table_wrapper'] = array(
'#type' => 'item',
'#markup' => myid_create_templates_table(),
);
The function below, populates the element above with the HTML of a table.
function myid_create_templates_table(){
//Retrieve list of templates in the database
$query = db_select('studidtemplates', 't1',array('target' => 'import'));
$result = $query
->fields('t1', array('template_id', 'template_name', 'date_created', 'user_name'))
->orderBY('template_id', 'DESC')
->execute();
$table_header = array(
'template_id' => array('data' => 'Template ID' , 'field' => 'template_id','sort' => 'asc'),
'template_name' => array('data' => t('Template Name'), 'field' => 'template_name','sort' => 'asc'),
'date_created' => array('data' => 'Date Created' , 'field' => 'date_created','sort' => 'asc'),
'user_name' => array('data' => t('Created by'), 'field' => 'user_name','sort' => 'asc'),
);
foreach ($result as $record) {
$table_rows[] = array(l(t($record->template_id), $record->template_id, array('attributes' => array('class' => 'use-ajax'))), $record->template_name, $record->date_created, $record->user_name);
}
return '<div id="myid_templates_editor_table_wrapper">' . theme('table', array('header' => $table_header, 'rows' => $table_rows)) . '</div>';
}
It looks like the image below:
Whenever a new record is inserted to the table, the function below renders the $form['myid_templates_editor_table_wrapper']
element to update it's table display.
function myid_save_templates_editor($form, &$form_state){
global $user;
$save = db_insert('studidtemplates',array('target' => 'import'))
->fields(array(
'template_name' => 'WEW',
'date_created' => date("Y-m-d H:i:s", time()),
'user_name' => $user->name
))
->execute();
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),
)
);
}
I notice something weird about it. When my actual record in the database is 1, the table displays 0 records. When my actual record is 2, my table displays 1 record. When my actual record is 3, it displays 2 records in my table and so on and so forth.
I was suspecting that my $form['myid_templates_editor_table_wrapper']
element was being rendered, even if db_insert
has not yet successfully inserted a record in my database.
Now, the question is : How will I execute the rendering of my form element after db_insert
had successfully inserted a record in my table?
UPDATE 1:
I tried Shabir A. 's suggestion:
$save = db_insert('studidtemplates',array('target' => 'import'))
->fields(array(
'template_name' => 'WEW',
'date_created' => date("Y-m-d H:i:s", time()),
'user_name' => $user->name
))
->execute();
watchdog('myid', $save);
if($save){
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),
)
);
}
But the problem still occurs, When I view my watchdog
table, the $save
variable has a value of 371
. That means it was inserted successfully, as what Shabir A.
said, but the table display was not updated. Is there anything wrong with my query? Where am I missing?