I have created a custom form in my module so that when the user clicks submit, some changes are performed in the databases and other actions follow. The form is an empty form with only the submit button nothing else. When the user clicks the Submit button, a function is called to perform some tasks. However, I am getting some errors. Here's the code:
My module folder is formtest and is placed in sites/all/modules/
formtest.info
name = Test Form
description = Accept Challenge Form Submit
core = 7.x
formtest.module
function formtest_menu() {
$items['formtest'] = array(
'title' => 'Accept Challenge',
'page callback' => 'drupal_get_form',
'page arguments' => array('formtest_form'),
'access callback' => TRUE,
);
return $items;
}
function formtest_form($form,&$form_state) {
$form['button'] = array(
'#type' => 'submit',
'#value' => 'Accept Challenge',
'#submit' => array('formtest_action'),
);
return $form;
}
function formtest_action($form,&$form_state) {
$query = db_insert('challenges')
->fields(array(
'cid' => '$node->nid',
'uid' => '$user->uid'
))
->execute();
}
/** * To display the form on a particular content type */
function formtest_node_view($node, $view_mode = 'full', $langcode = NULL) {
if($view_mode == 'full') {
if($node->type == 'challenge') {
$node->content['my_form'] = array(
'#markup' => drupal_get_form('formtest'),
'#weight' => 1,
);
}
}
}
Well that's all. There's no form showing up..no error messages..nothing...I tried a var_dump() but nothing...That custom content page is displaying as it was but there seems to be no effect of this code. Would appreciate your help in any manner. Thanks