In a custom module, I have a form allowing some Create/Update/Delete data in a specific table of the database.
In the submit part of the code, I'd like to compare 'before' state (what was displayed to the user) to 'after' state (what he has actually entered) in order to properly update the database: create what has been added, update what has been changed and delete what has been removed. How can I do that?
By the way, the site is running Drupal 6.37.


First of all: You should migrate to Drupal 7 or 8. Drupal 6 is no longer supported as of Feb 24th, 2016. If migration is not possible, perform security updates. The latest version of Drupal 6 is 6.38 released on 24 February 2016. Have you also heard about the Drupal 6 LTS initiative? I highly recommend reading the details https://www.drupal.org/project/d6lts, https://github.com/d6lts/drupal.

Answering your question, the simplest thing would be to pass the object with the data in the form:

$form['my_object'] = array(
  '#value' => $my_object,
  '#type' => 'value',

$form['some_field'] = array(
  '#default_value' => $my_object->some_field,
  '#type' => 'textfield',

Then in the submit handler you can compare values:

if ($form_state['values']['my_object']->some_field != $form_state['values']['some_field']) {
  // Value of some_field has changed!
  • Migration to D8 is on its way. Takes time... And I don't have much time for this activity. Hence the delay :-( I'll try your advice. – gbmapo Dec 13 '18 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.