By SidneyGijzen on
Hey,
I started working on a (my first) custom form module, which purpose is to edit existing non-Drupal database tables. The current code is based on examples from the Pro Drupal Development book and this post I found on drupal.org. After 2 days of struggling to get the submit function working, I was hoping maybe one of you guys could point me in the right direction.
The contents of my .module is as follows:
// $Id$
/**
* @file
* Module for editing db-tables with maintenance objects.
* Inspiration and major part of the code: http://drupal.org/node/1019526
*/
/**
* Implementation of hook_perm().
*/
function form_ldl_perm() {
return array('query database');
}
/**
* Implementation of hook_menu().
*/
function form_ldl_menu() {
$items['landgoed/landgoed-de-laan/bevraag-db'] = array(
'title' => 'Wijzig beheerobjecten',
'page callback' => 'form_ldl_query_db',
'access callback' => 'user_access',
'access arguments' => array('query database'),
'type' => MENU_NORMAL_ITEM,
);
$items['landgoed/landgoed-de-laan/bevraag-db/edit/%beheerobject'] = array(
'title' => t('Edit Form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('beheerobject_edit_confirm', 4),
'access arguments' => array('query database'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Page callback for the overview page where you can select maintenance objects.
*/
function form_ldl_query_db() {
$header = array(t('Code'), t('Waterkeur'), t('Lengte'), t('Breedte'));
$query = "SELECT * FROM {ldlwlijn}";
$rs = db_query($query);
$row = array();
if ($rs) {
while ($data = db_fetch_object($rs)) {
$code = $data->code;
$waterkeur = $data->waterkeur;
$lengte = $data->lengte;
$breedte = $data->breedte;
$row[] = array($code, $waterkeur, $lengte, $breedte, l(t('Edit'), "landgoed/landgoed-de-laan/bevraag-db/edit/$data->code"));
}
}
$str .= theme_table($header, $row);
return $str;
}
/**
* Implementation of hook_load() (as far as I know now)
*/
function beheerobject_load($beheerobject) {
$query = "SELECT * FROM {ldlwlijn} WHERE code = '%s'";
$rs = db_query($query, $beheerobject);
if ($rs) {
while ($data = db_fetch_object($rs)) {
return $data;
}
}
return FALSE;
}
/**
* Function called on load of the edit form page.
*/
function beheerobject_edit_confirm(&$form_state,$beheerobject){
$form = array();
$form['waterkeur']=array(
'#title'=>t('Waterkeur'),
'#type'=>'textfield',
'#default_value' => $beheerobject->waterkeur
);
$form['lengte']=array(
'#title'=>t('Lengte'),
'#type'=>'textfield',
'#default_value' => $beheerobject->lengte
);
$form['breedte']=array(
'#title'=>t('Breedte'),
'#type'=>'textfield',
'#default_value' => $beheerobject->breedte
);
return confirm_form($form,t(''),
isset($_GET['destination']) ? $_GET['destination'] : "landgoed/landgoed-de-laan/bevraag-db",
t(''),
t('Edit'),
t('Cancel'));
}
/**
* Implementation of submit function. (function name + _submit)
*/
function beheerobject_edit_confirm_submit($form, &$form_state)
{
if ($form_values['confirm']) {
$waterkeur = $form_state['values']['waterkeur'];
$lengte = $form_state['values']['lengte'];
$breedte = $form_state['values']['breedte'];
$query = "UPDATE {ldlwlijn} SET waterkeur = '$waterkeur', lengte= '$lengte', breedte = '$breedte' WHERE code='%s'";
$rs = db_query($query, $beheerobject->code);
}
$form_state['redirect'] = "landgoed/landgoed-de-laan/bevraag-db";
}
I'm a newbie to custom module development, so if you need more info, please don't hesitate to ask. I would be very grateful if someone could point into the right direction!
Thank you in advance!
MF
Comments
Check below code if it works
Check below code if it works or not :
Might it helps...
Abhishek Sawant
Drupal Developer
Awesome!
It sure was of help! After some small changes to your code (since my id is a textfield, i put the "id" en "code" variables in quotes here and there), it works perfectly.
I need to study your code a bit more, so I can understand exactly what you're doing and where. I hope (if needed), I can ask you some questions about it in this topic in the future about it. :)
For now; thanks a LOT! You really made my day!
MF
Yes sure, Its my pleasure to
Yes sure, Its my pleasure to help..:)
Abhishek Sawant
Drupal Developer
Implementing in Drupal
Hi, i have attached a Zip a file... with my functionality including Db files...... Please help me, i have to do this same functionality in Drupal 7.8... How can i implement this... Please i have tried but not yet..
i am new to drupal, Please help its urgent for me.... http://drupal.org/node/1288472 Please chk this link for my attach file...
Thanks a lot
hey, I would like to help
hey,
I would like to help you, but unfortunately you're developing in Drupal 7 (D7). The form which I'm using is suited for Drupal 6. I heard quite a lot has changed in the API for D7 and since I haven't worked with D7 yet, I'm not able to help you at this moment in time.
I suggest you start a new thread in this forum (clearly marking it as D7).
MF
Hii...Thanks for ur prompt
Hii...Thanks for ur prompt response on this... Please guide me for that in 6 what u know at present so that i will try to work on 7... based on that.
Thank u soo.. much
Look at the second post in this topic....
Well...actually the second post in this topic (from Abishek) is a piece of code which works perfectly. So, you can compare that to your code. Just think in your tables and fields...
However, again, the Form API has changed quite a lot in 7, see this series of posts of fellow drupaller Randy Fay
Fixed that problem...
Hii.. it is successfully running for me.. thanks for ur help... i have an another doubt on this we have a select drop down in user form... Whatever the user selects, i want to get that value and also i want to get text field value too to compare Both values in the Data Base. Is there any easy way to this... Or else how do we do this to get that values
Please help me.. Thanks once again..
You're welcome!
You're welcome. Good to hear you got it working.
In the Drupal 6 Form API you can build select lists as follows...
Include the following in your form
To retrieve the selected value on submit, include the following in your submit function
So, the variable "$example" has the selected value and can be used in your update or insert statement for the db_query.
I hope this example will give you enough to go on and get it working in D7!
Thanks for ur great help
Thanks for ur great help friend..... Both user and admin forms are working very well...... Thanks for ur Suggestions....
You're welcome!
Good to hear you got everything working! :)
Are you sure the 'EDIT'
If I remove the if ($id) in this I can get the edit to work. I've changed the pathing and I'm wondering how you are passing the id var from the 'EDIT' click to this function. I think that is where I'm getting fouled up.
Refer form_ldl_form()
Refer form_ldl_form() function where we passing an hidden id on edit condition.
Abhishek Sawant
Drupal Developer
I see the hidden field, I see
I was having problems with the ?q= portion of the url in my WAMP install. Testing on my stage server shows that this is working. Thanks for the heads up of where to look for the var, that got me to the answer.
Hii
In Wamp server by default it enables clean urls... So.. just disable clean urls in site configuration. For better, If u use Xampp then u never face this issue..
Events
Hii...... Please help me if you have any idea about this...
I implemented Upcoming Events in version 6. now if i want to display the selected events in "Whats New" Block then what should i do..? Any Idea ?
_
This is not a code question and has nothing to do with the topic of this post-- please don't hijack others' threads. They're free-- create your own for the new topic in the appropriate forum (only code questions belong in this forum).