3

Below are the four required files to install a module that I created. They are all located within the '/third_party/database_add/' folder. My issue and my question is located after these files, which I am including to show what I've done so far.

File: database_add_lang.php

Location: /third_party/database_add/language/english/

$lang = array(
'my_module_module_name'     => 'Database_add',
'my_module_module_description'  => 'Contains functionality to add a user to the user table',
''=>''
);

File: mcp.database_add.php

Location: /third_party/database_add/

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Module_name_mcp {
    }
/* End of file mcp.module_name.php */
/* Location: ./system/expressionengine/third_party/database_add/mcp.database_add.php */

File: mod.database_add.php

Location: /third_party/database_add/

class Database_add{
    var $return_data = '';

    function register(){
        $username = "john"
        $passhash = "smith"
        $data = array(
            'User_Name'     =>$username,
            'User_Passhash' =>$passhash
        );
        $query = ee()->db->insert('exp_user', $data);
    }

    function form(){
      $return_data = ee()->functions->form_declaration(array(
        'hidden_fields' => array(
          'ACT' => ee()->functions->fetch_action_id('Database_add', 'register'),
        ),
      ));

      $return_data .= ee()->TMPL->tagdata;
      $return_data .= '</form>';
      return $return_data;
    }
}

File: upd.database_add.php

Location: /third_party/database_add/

class Database_add_upd{
    var $version = '1.0';

    function install(){
        $data = array(
            'module_name'       => 'Database_add',
            'module_version'    => $this->version,
            'has_cp_backend'    => 'n',
            'has_publish_fields'=> 'n'
        );
        ee()->db->insert('modules', $data);

        $data = array(
            'class'     => 'Database_add',
            'method'    => 'add_user'
        );
        ee()->db->insert('actions', $data);
    }

    function uninstall(){
        ee()->load->dbforge();

        ee()->db->select('module_id');
        $query = ee()->db->get_where('modules', array('module_name' => 'Database_add'));

        ee()->db->where('module_id', $query->row('module_id'));
        ee()->db->delete('module_member_groups');

        ee()->db->where('module_name', 'Database_add');
        ee()->db->delete('modules');

        ee()->db->where('class', 'Database_add');
        ee()->db->delete('actions');

        return TRUE;
    }

    function update($current = ''){
        return FALSE;
    }
}

That is my file structure. I followed this EllisLab tutorial to get this far. My front-end form code looks like this:

{exp:database_add:form}
    <input class="text_box" type="text" name="username" id="username" />
    <input class="text_box" type="password" name="password" id="password" />
    <input type="submit" name="submit_button" value="Register" />
{/exp:database_add:form}

The above form is simplified of course. The form successfully renders as this:

enter image description here

Now take a look at my register() function. No matter what I enter into the above form, I expect register() to fire, and the username "john" with the passhash "smith" to be entered into the "exp_user" table. That doesn't happen. The table remains empty. All that happens is my blank 'index.php' page loads.

My question is: How do I get the register() function to fire when I press the Submit button on the form?

2

The issue was with my a specific line of code in my upd.database_add.php file. The original line was

'ACT' => ee()->functions->fetch_action_id('Module_name', 'register'),

It should be (and currently is corrected to)

'ACT' => ee()->functions->fetch_action_id('Database_add', 'register'),

I forgot to put the name of my module as an argument in the fetch_action_id() function. It was searching for the action ID of a function called 'register', but it was trying to look in the wrong module 'Module_name', which doesn't even exist. No error message or anything.

0

It looks like you'll need to also add the XID Hash into your form hidden input with the name "XID" as well for it to fire.

I would suspect that's the issue, but can we see what the rendered form in the front end looks like?

Carl

6
  • You can also turn off "Secure Form" to verify that it's an XID issue. XID can be done line this: $XID = (isset($_POST['XID']) == TRUE) ? $_POST['XID'] : ''; EE will do the rest for you. – Manuel Payano Sep 3 '13 at 14:48
  • I just edited my submission with the screenshot of the rendered form. I'll give the XID hidden field and get back to you about the results in a few minutes. – John G Sep 3 '13 at 14:51
  • Okay, I did a quick google fu and added the following line to my form: <input type="hidden" name="XID" value="<?=XID_SECURE_HASH?>" /> I don't know if that's all I had to do, I am brand new to EE. Anyway, still the register() function did not fire. Edit: In addition, I just checked the page source. <?=XID_SECURE_HASH?> didn't even render to a string, it displays as the literal value of the 'value' attribute. – John G Sep 3 '13 at 14:58
  • Did you disable secure forms as i suggested? It's a quick way to know if this is really a XID issue. (remove the XID field you just added) CP Home > Administration > Security and Session Preferences > Process form data in Secure Mode? – Manuel Payano Sep 3 '13 at 15:14
  • I suppose it's not an XID issue then. I commented out the hidden field, and disabled secure mode. I refreshed the page and pressed the submit button. Still no record was added to the database. – John G Sep 3 '13 at 15:21
0

I don't have any docs, but you could look at my Authenticate add-on for a real world example. This form building process has always been a pain for me, so I built Base Form to make life easier. Your forms will inherits loads of default functionality without you having to code it. This library is due for a 2.0 rewrite at some point, and not perfect, but even still I use it all the time with almost all of my add-ons.

https://github.com/objectivehtml/Base-Form

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.