• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Discover User ID after form process

SoilentRed

Caaan do!
I'm going to use the Juser form plugin so that my team leaders can add members to the site and their group. I'm using the PHP form plugin to insert a row into a table (onAfterProcess) that has: groupid, memberid, approved and permissions - (This is for JomSocial _community_groups_members table).

I assume that when the form is submitted, it simply autoincrements the userid the for the Joomla _users Table. I need to be able to capture the new user's ID and add it to the insert values as memberid for the _community_groups_members table.

How, I ask you... HOW?!?!?

Is there a way I can have an element capture the new userid on submit?

You're help is valued and appreciated.
 
When setting up the juser plugin, you have to specify the element on the form that holds the newly assigned user ID. That element should then contain that ID when your onAfterProcess code runs, so in (say) $formModel->formData['userid'] or $formModel->formDataWithTableName['yourtable___userid'].

(NOTE that onAfterProcess, entries in $formModel->formData[] have had the tablename___ prefix removed, but there's a copy of the data that still has them, formDataWithTableName)

-- hugh
 
You're solution was so simple I had a hard time accepting it. For other novices like myself, here's exactly what i did.
  • Create a field element called "whatever_you_want". I have Eval set to "No"
  • Using the juser form plugin, be sure that the user id is assigned to "whatever_you_want".
  • Using the php form plugin, for the "Process Script" dropdown, select "End of form submission (onAfterProcess)".
Here is the code I used:
Code:
$newuserid = $formModel->formData['whatever_you_want'];
return $newuserid;

Easy-Peezy-Lemon-Squeezy.

Thanks Hugh for the valuable guidance.
 
I'm trying to insert this information into a row on a separate table, but it doesn't take.

$newuserid = $formModel->formData['new_user_id'];
return $newuserid;

// Get a db connection.
$myDb = JFactory::getDbo();

// Create a new query object.
$myQuery = $myDb->getQuery(true);

// Insert columns.
$columns = array('groupid', 'memberid', 'approved', 'permissions');

// Insert values.
$values = array('{general_registration___jomsocial_user_group_id}', '$newuserid', '1', '1');

// Prepare the insert query.
$myQuery
->insert($myDb->quoteName('xxxxx_community_groups_members'))
->columns($myDb->quoteName($columns))
->values(implode(',', $myDb->quote($values)));


// Reset the query using our newly populated query object.
$myDb->setQuery($myQuery);

try {
// Execute the query
$result = $myDb->execute();
//use $myDb->query() in Joomla2.5
}
catch (Exception $e) {
// catch any database errors.

}
$newid = (int)$myDb->insertid(); //get new record id

Can you help me out? I think it's just the markup.
 
OK, first problem is that on line 2, you do ...

return $newuserid;

... which means the script stops there, and returns that value to whatever called it. So delete that line.

Then in this line:

Code:
// Insert values.
$values = array('{general_registration___jomsocial_user_group_id}', '$newuserid', '1', '1');

... you have $newuserid in single quotes. In PHP, that prevent variables from being evaluated, see the first answer here:

https://stackoverflow.com/questions...ingle-quoted-and-double-quoted-strings-in-php

... so that would literally write $newuserid to the table, not the value of the PHP variable.

And, for reasons unknown, $db->quote() doesn't work like $db->quoteName(). You can't do $db->quote($arrayOfValues) the same way you can do $db->quoteName($arrayOfNames). You have to explcitily quote the values in the array, then just implode it.

Code:
// Insert values.
$values = array(
    $myDb->quote('{general_registration___jomsocial_user_group_id}'),
    $myDb->quote($newuserid),
    $myDb->quote('1'),
    $myDb->quote('1')
);

Code:
->values(implode(',',$values));

Note that Fabrik placeholders are different to PHP variables. We replace those before handing your code to PHP to execute, so what PHP sees is 'whatever you variable was'.

-- hugh
 
I appreciate it Hugh, but this still isn't inserting the row.
Code:
<?php 

$newuserid = $formModel->formData['new_user_id'];

// Get a db connection.
$myDb = JFactory::getDbo();

// Create a new query object.
$myQuery = $myDb->getQuery(true);

// Insert columns.
$columns = array('groupid', 'memberid', 'approved', 'permissions');

// Insert values.
$values = array(
  $myDb->quote('{general_registration___jomsocial_user_group_id}'), 
    $myDb->quote($newuserid), 
    $myDb->quote('1'), 
    $myDb->quote('1')
    );

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',',$values));

// Reset the query using our newly populated query object.
$myDb->setQuery($myQuery);

try {
// Execute the query
  $result = $myDb->execute();
//use $myDb->query() in Joomla2.5
}
catch (Exception $e) {
// catch any database errors.

}
$newid = (int)$myDb->insertid(); //get new record id


?>
 
OK, so now it's time to debug.

Put ...

var_dump((string)$myQuery);exit;

... just before the execute. See what it shows.

Depending what element type new_user_id is, you may find that it's an array.

-- hugh
 
Sorry for the belated here. I took an unearned vacation. I'm back now. I put the php form element like so:

Code:
<?php 

$newuserid = $formModel->formData['new_user_id'];

// Get a db connection.
$myDb = JFactory::getDbo();

// Create a new query object.
$myQuery = $myDb->getQuery(true);

// Insert columns.
$columns = array('groupid', 'memberid', 'approved', 'permissions');

// Insert values.
$values = array(
  $myDb->quote('{general_registration___jomsocial_user_group_id}'), 
    $myDb->quote($newuserid), 
    $myDb->quote('1'), 
    $myDb->quote('1')
    );

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',',$values));

// Reset the query using our newly populated query object.
$myDb->setQuery($myQuery);
var_dump((string)$myQuery);exit;
try {
// Execute the query
  $result = $myDb->execute();
//use $myDb->query() in Joomla2.5
}
catch (Exception $e) {
// catch any database errors.

}
$newid = (int)$myDb->insertid(); //get new record id


?>

Note: it does capture the new user id and insert it into the table, but it doesn't insert the row into the jomsocial user table (xxxxx_community_groups_members).

Here is what I assume the relevant part of the debugger spits out when viewing the form:

Code:
                            [element_ro] => 
                            [value] => 
                            [element_raw] => 
                            [dataEmpty] => 1
                            [labels] => 0
                            [dlabels] => 0
                            [tipAbove] => 
                            [tipBelow] => 
                            [tipSide] => 
                            [offset] => 0
                            [span] => span12
                            [column] =>  style="clear:both;width:100%;"
                        )

                    [new_user_id] => stdClass Object
                        (
                            [startRow] => 1
                            [endRow] => 1
                            [error] => 
                            [plugin] => field
                            [hidden] => 1
                            [id] => general_registration___new_user_id
                            [className] => fb_el_general_registration___new_user_id
                            [containerClass] => fabrikElementContainer plg-field fb_el_general_registration___new_user_id fabrikHide 
                            [element] => 
<input
    type="hidden" id="general_registration___new_user_id" name="general_registration___new_user_id" class="input-medium form-control  hidden fabrikinput inputbox text" value=""  />

                            [label_raw] => New User ID
                            [label] =>     New User ID
                            [errorTag] => <span class="fabrikErrorMessage">


</span>

                            [element_ro] => <!--  -->
                            [value] => 
                            [element_raw] => 
                            [dataEmpty] => 
                            [labels] => 0
                            [dlabels] => 0
                            [tipAbove] => 
                            [tipBelow] => 
                            [tipSide] => 
                            [offset] => 0
                            [span] => span12
                            [column] =>  style="clear:both;width:100%;"
                        )

                )

            [subgroups] => Array
                (
                )

            [startHidden] => 
            [repeatIntro] => 
            [class] => fabrikGroup form-horizontal
            [newGroup] => 
        )

)
 
If it's showing the usual page with Fabrik debug, then your code isn't running. It should show an otherwise blank page with just your custom query on it.

Is this code in a file, or in the PHP box?

If it's in the PHP box, get rid of the enclosing <?php and ?> tags.

-- hugh
 
I put this without the php tags into the php form plugin (onAfterProcess). I tried it two ways.
Code:
// Get a db connection.
$myDb = JFactory::getDbo();

// Create a new query object.
$myQuery = $myDb->getQuery(true);


$newid = (int)$myDb->insertid(); //get new record id

$newuserid = $formModel->formData['new_user_id'];
return $newuserid;

// Insert columns.
$columns = array('groupid', 'memberid', 'approved', 'permissions');

// Insert values.
$values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',', $myDb->quote($values)));

// Reset the query using our newly populated query object.
 $myDb->setQuery($myQuery);
try {
// Execute the query
var_dump((string)$myQuery);exit;
  $result = $myDb->execute();
//use $myDb->query() in Joomla2.5
}
catch (Exception $e) {
// catch any database errors.

}

and like this

Code:
// Get a db connection.
$myDb = JFactory::getDbo();

// Create a new query object.
$myQuery = $myDb->getQuery(true);


$newid = (int)$myDb->insertid(); //get new record id

$newuserid = $formModel->formData['new_user_id'];
return $newuserid;

// Insert columns.
$columns = array('groupid', 'memberid', 'approved', 'permissions');

// Insert values.
$values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',', $myDb->quote($values)));

// Reset the query using our newly populated query object.
 $myDb->setQuery($myQuery);
var_dump((string)$myQuery);exit;
try {
// Execute the query
  $result = $myDb->execute();
//use $myDb->query() in Joomla2.5
}
catch (Exception $e) {
// catch any database errors.

}

Should I be placing this or the vardump snippet you gave me somewhere else?
 
Erm ... that code is totally different to what I gave you.

I gave you:

Code:
$newuserid = $formModel->formData['new_user_id'];

... you have ...

Code:
$newid = (int)$myDb->insertid(); //get new record id

$newuserid = $formModel->formData['new_user_id'];
return $newuserid;

... which is a) getting a meaningless insertId(), and b) returning $newuserid. In other words, your code stops there, and returns that value to the calling code. Nothing else gets executed.

-- hugh
 
I'm a dumbass. :rolleyes:. I owe you a drink if i ever see you.

Appreciate your patience here. After making the adjustment and using the vardump, this is what i get:

Code:
C:\wamp64\www\harvey-app\plugins\fabrik_form\php\php.php(485) : eval()'d code:27:string '
INSERT INTO `xxxxxxx_community_groups_members`
(`groupid`,`memberid`,`approved`,`permissions`) VALUES
('','\'1406\'','1','1')' (length=128)

Sooo
there's progress because now it's inserting a new row, into xxxxxx_community_groups_members, but the values are incorrect. This is what is actually getting entered into phpmyadmin

Code:
groupid        memberid    approved    permissions
0            0               1                   1
 
Again, look at the code I gave you ...

Code:
// Insert values.
$values = array(
  $myDb->quote('{general_registration___jomsocial_user_group_id}'), 
    $myDb->quote($newuserid), 
    $myDb->quote('1'), 
    $myDb->quote('1')
    );

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',',$values));

... vs what you have:

Code:
// Insert values.
$values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

// Prepare the insert query.
$myQuery
    ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',', $myDb->quote($values)));

-- hugh
 
I could add a new row on different table using given instructions. Thank you, Hugh. :)

How do I add an additional check / clause to this code, where Fabrik will check first whether the User is already existed in the another table, if it is already existed the query should stop or update the existing values. I have enabled the PHP require ONCE in the form plugin, but if User Refreshes the Page, it creates/inserts one more UserRow in the third Table :(
 
The "require once" only controls where we include your file with a 'require' or a 'require_once' in PHP, so whether the code gets run more than once on a given page load, not whether it only ever runs once.

If you are inserting into the J! users table, such that the userid is the primary key, then you could use a "INSERT ... ON DUPLICATE KEY UPDATE" statement.

If not, you'll need to do it by hand, do a select on that userid, then issue an insert if you don't get a result, and an update if you do.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top