Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Yaeko-1 on
Hi,
I've create a module with a custom form in the configuration panel.
Now in my form there are some fields that need to be saved in my DB (created when activating my module). I've searched for a hook that should save the form, but didn't find anything usefull yet.
This is my code for showing the form:
function fb_recommend_form($form, &$form_state){
$form = array();
$form['url'] = array(
'#type' => 'textfield',
'#title' => 'Url to recommend',
'#description' => t('Enter the url of the site that you want to use for the Facebook Recommnendation box'),
'#size' => 50,
'#maxlength' => 255,
'#required' => TRUE
);
$form['height'] = array(
'#type' => 'textfield',
'#title' => t('The height of the Facebook Recommendation box'),
'#description' => t('Enter the desired height (without pixels or px) of the Facebook Recommendation box'),
'#size' => 10,
'#maxlength' => 3,
'#required' => FALSE
);
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('The width of the Facebook Recommendation box'),
'#description' => t('Enter the desired width (without pixels or px) of the Facebook Recommendation box'),
'#size' => 10,
'#maxlength' => 3,
'#required' => FALSE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
Anyone knows which hook I can use to save this form?
Thanks!
Comments
You use the form definition
You use the form definition function name and add the suffix _submit.
Your form definition function name is: fb_recommend_form()
So your submit function would be: fb_recommend_form_submit()
You can also have a validation function: fb_recommend_form_validate()
Ok cool, I found it thanks,
Ok cool, I found it thanks, but I'm getting an error when saving:
error:
Not sure if it has to do with my column properties?
Never mind, found it :)
Never mind, found it :)
how did u managed to get rid of this errror
tice: Undefined index: outscript in helloworld_form_submit() (line 55 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'outscript' cannot be null: INSERT INTO {helloworld} (serverurl, inscript, outscript) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 2333 [:db_insert_placeholder_1] => dfsf [:db_insert_placeholder_2] => ) in helloworld_form_submit() (line 64 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
how did u managed to get rid of this errror
otice: Undefined index: outscript in helloworld_form_submit() (line 55 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'outscript' cannot be null: INSERT INTO {helloworld} (serverurl, inscript, outscript) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 2333 [:db_insert_placeholder_1] => dfsf [:db_insert_placeholder_2] => ) in helloworld_form_submit() (line 64 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
He didn't have that error.
He didn't have that error.
Thanks...
do we need to create the table?
amazing_forms <- do i need to create this table?
Is this approach still valid
Is this approach still valid in 2022? To write directly into DB?