Hi,

I am developing a module that uses a form with many fields and enters them into a table in Mysql. I wanted to use the same form to edit already entered values from the table. How do I enter the values I have fetched from the database into the form ?

Thanks a lot in advance

Regards

Karthik

Comments

nevets’s picture

You do relize you can do that with CCK and not a module, just thought I would ask. Also if this is a content type, $node passed to the form hook holds the existing values.

But to answer your question, drupal_get_form() can take more than one argument and additional arguments are passed to the function that builds the form.

So lets say your form function looks like

function your_module_your_form($record = NULL) {
  ..
}

It is the addition of $record that is usefull. With it you can use $record->field for a default value (obviously 'field' needs to be real field name). The 'trick' now is to define a second menu entry for editing existing content and pass the id of the existing content as part of the url (look at the way node/add and node editing works). The callback function can then use the id to load the record and call drupal_get_from() something like this

call drupal_get_from('your_module_your_form', $record) ;
ponkarthik’s picture

Dear Nevets,

Thanks for your speedy reply.

This may sound silly. Should I manually set the '#value' attribute for each field ? Is there no easy way ?

By the way my form has many fields including lots of checkboxes and radios etc. and they are entered into a separate mysql table. It is not a node type. Is it possible to do all this using CCK ?

Thanks

Karthik

nevets’s picture

First can you do it all with CCK possibly, particulary if you do not need things in separate tables (they may end up in separate tables, but CCK gets to choose).

As for '#value', use '#default_value' to set the intial value of form fields. And a warning, using '#value' in place of '#default_value' will lead to hours of frustration as you try and figure why the values you entered are not getting saved.

geek’s picture

Please :-)

Many many people will have searched for a clue in vain until they did a search and read the answer above. :)