So, I've been working at this and searching forums for quite some time and have tried many different options and still can't figure out what the issue is. I've been working with PHP for several years, but I'm still pretty new to Drupal. I'm working on some code that I want to simply take input form a form then insert into a database table called 'dr_rower_results', my code is below:
<?php
/**
* @file
* This module allows users to input information for rower race results.
*/
$form = drupal_get_form('rower_results_form');
/**
* Implements hook_form().
*/
function rower_results_form($form, &$form_state){
$form = array();
$form['overview'] = array(
'#markup' => t('This form allows you to enter results for rowers'),
'#prefix' => '<h3>',
'#suffix' => '</h3>',
);
$form['name'] = array(
'#title' => t('Rower\'s Name'),
'#description' => t('Enter the rower\'s name'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['team'] = array(
'#title' => t('Rower\'s Team'),
'#description' => t('Enter the team the rower rows for'),
'#type' => 'textfield',
'#required' => TRUE,
);
//$form['actions'] = array('#type' => 'actions');
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#submit'][] = 'rower_results_form_submit';
return $form;
}
/**
* Validate Rower Results admin settings
*/
function rower_results_form_validate($form, &$form_state){
//dpm($form_state);
echo("<script type='text/javascript'>alert('validate');</script>");
// Regular expression for validating
//$name_regex =
//$name = $form_state['values']['rower_results_name'];
//if(!preg_match($name_regex, $name)){
// form_set_error('rower_results_name', t('invalid name'));
//}
}
function rower_results_form_submit($form, &$form_state){
$result = db_insert('dr_rower_results')
->fields(array('name' => $form_state['values']['name'],
'team' => $form_state['values']['team']
))->execute();
drupal_set_message(t('Your form has been submitted'.$args[0].$args[1]));
}
function hook_block_view($delta = ''){
}
echo(drupal_render(&$form));
?>
This code is in a block with text format set as PHP code. I believe I have narrowed this down to it being an issue with the db_insert (also tried db_query('INSERT INTO... ). If I remove the db_ function then the form clears and shows the message "Your form has been submitted" followed by my two inputs. If I keep the db_ function there then on submit the page simply brings me to my drupal user page and does not make an insert into the database.
Any help would be much appreciated.
Comments
You should be putting this in
You should be putting this in a module, not using the PHP filter on a block. I don't think you can declare form functions with the PHP filter.
If you create a module, you can create a block using hook_block_info() and hook_block_view(). You can use your form in that block.
Thanks!
Finally got this working in a module. A couple of problems I ran into in case anybody is as new to this as me:
rower_results_form doesn't work because in the .info file the name is = Rower Results
Rower_Results_form solves that problem
db_insert('dr_rower_results') does not work
db_insert( ) automatically adds 'dr_' to the start of the node name, so simply writing:
db_insert('rower_results') when the node is named 'dr_rower_results' works
Also, while a MySQL query may allow an "INSERT INTO" without including values for all fields, the Drupal db_insert( ) requires that ALL fields are specified (unless they have a default value)
Anyways, thanks Jaypan for the help, and I hope this helps somebody who's as lost as I was a week ago!
Here is my working code for anyone interested:
Glad you got it working,
Glad you got it working, however there is one issue. You should not use uppercase module machine names, which means the folder that contains the module, as well as all files. One reason is the issue you ran into - if you use capital letters, you have to do the same with your function names. But the other is that some hooks will not work with capital letters in the name. hook_update() is one I believe. This can be really hard to debug, so you would be doing yourself a favor to only use lower-case letters in your module machine names.
So you should rename your module folder to rower_results, and name the files inside accordingly. Note that you should disable and uninstall the module before this, and reinstall afterwards. You cannot do it while the module is enabled on the site.