Results 1 to 6 of 6

Thread: processFormData Hook to Add Values to Database Post

  1. #1
    New user
    Join Date
    10-11-09.
    Posts
    9

    Default processFormData Hook to Add Values to Database Post

    I'll admit that I'm a bit over my head here. I'm picking up the PHP on the fly, but still pretty clueless.

    I have set up a form for user input, and I have set up functions to manipulate those entries to create data I would like inserted into the same row on the database. I have set up a processFormData hook and created a function that will output the values that I want, but because the hook fires after the form data has been inserted, I can't figure out how to get them into the database. I wrote an update query, but don't know how to get it to work. It looks something like this:

    "UPDATE tl_mytable SET field1 = '$var1', field2 = '$var2', field3 = '$var3'... WHERE submittedField1 = 'submittedValue1' AND submittedField2 = '$submittedValue2'"

    I use the conditional WHERE with a couple of the values just submitted to identify the correct record. I know that there is a way to access the current record in some circumstances, but I don't know if that applies here or how to use it if it does.

    Thanks in advance.

  2. #2
    New user
    Join Date
    10-11-09.
    Posts
    9

    Default Workaround for this problem?

    This problem seems to be more complicated than I expected.

    I am looking for a way to use a front end form to gather data, and then I want to process that data using some simple functions to add additional data to the different fields within the same record in the database.

    For example, tl_myTable has 5 fields: field_1, field_2, field_3, field_4, field_5. In the front end myForm gets values from the user for fields 1,2, and 3, and then I use myFunction1($field_1) and myFunction($field_2) to obtain values for fields 4 and 5.

    I have tried several different approaches to this, but the underlying code that generates and processes the pages and forms makes it difficult to change the form or create my own custom form.

    Any ideas?

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: processFormData Hook to Add Values to Database Post

    Code:
    processFormData ¶
    The processFormData hook is triggered after a form has been submitted. It passes the form data array, the Data Container Array and the files array as arguments and does not expect a return value. It is available from version 2.4.4.
    1.// config.php 2.$GLOBALS['TL_HOOKS']['processFormData'][] = array('MyClass', 'myProcessFormData'); 3.  4.// MyClass.php 5.public function myProcessFormData($arrPost, $arrForm, $arrFiles) 6.{ 7.    // Do something 8.}
    I've not used this hook, but your user input into fields 1 2 and 3 are somewhere in ($arrPost, $arrForm, $arrFiles), as is the record id or any hidden fields etc in your form.

    The first step is to understand what is in the arrays passed....
    I'm not sure but $arrPost['field1'] should contain the posted value of field1.
    I don't know what $arrForm or $arrFiles contain. nor do I know where the record id is, but its in there somewhere.
    You'll need to print them out to see...
    Then update the database for that record...

    Your hook...

    Code:
    class MyHookClass extends Backend
    {
        public function myHookFunction($arrPost, $arrForm, $arrFiles) 
        {
              if ($arrPost['FORM_SUBMIT'] == 'auto_my_form_id')
              {
    
               $manipulatedData1 = arrPost['field1'].'manipulated';
               $manipulatedData2 = arrPost['field2'] + arrPost['field3'];
    
             $this->Database->prepare("UPDATE tl_myTable SET field4=?,field5=? WHERE id=?"
            ->limit(1)
            ->execute($manipulatedData1,$manipulatedData2,theRecordId);
    
              }
        }
    }
    Something like that

  4. #4
    New user
    Join Date
    10-11-09.
    Posts
    9

    Default Re: processFormData Hook to Add Values to Database Post

    When I try to run that hook, I get:

    "Fatal error: Call to a member function prepare() on a non-object in myHook.php" on the line that starts

    $this->Database->prepare("UPDATE myTable...

    Update:
    I put quotes around "myTable", and now I am getting a parse error on that same line.

  5. #5
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: processFormData Hook to Add Values to Database Post

    Not sure but....
    Does your hook class extend backend?
    Code:
    class MyHookClass extends Backend
    otherwise try
    Code:
    $something = $this->Database->prepare
    (probably this)

    otherwise
    Code:
    $this->import('Database');
    before the database call [note: edited]

  6. #6
    New user
    Join Date
    10-11-09.
    Posts
    9

    Default Re: processFormData Hook to Add Values to Database Post

    This seems to have done the trick:
    Code:
    $this->import('Database');
    Thank you so much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •