Read DB fields from Data Table in Form for specific user that created data

ITadminguy 07 Jul, 2023
New to CF7. Trying to read data, edit and save data, like I could do in CF5.

Problem - The form (on outside of website) works, new registered user created,and all data issaved.
I copied the form and made it a registered form to be used (on inside of website) so that the new user can log in, see the data on the first (public) form it originallly submitted, and edit and re-save that date on the second (registered user) form.

The problem is that I cant use the CF5 "getuser" code in CF7 so that each new user cn see, edit, and re-save their own data.

Instead, for all new users, oonly the first row of data for the first new user (user1) is showing up for all new users (e.g. user2, user3, user4, etc..

I need the db read to allow each user to only see, edit, and re-save their own data.

***********

Form 1 -
I created a basic contact (advanced) form that willd be used to add a new user on the front end.
I created the new table called "cf_table" using the Form=>Settings=>admin=>installer. I created data table olumns for id, first_name, last_name, email, nam, and [username. (there is another forum post that explains this. (Id field is int(11) + primary key_ auto increment), and all other fields are varchar with differing lengths)

Checked in myphpadmin thae table was reated.
Then deleted installer and clicked "connected table" and selected the new table "cf_table."

Created these fields
[u]Vuew edutor fields[/u]
id (hidden)
first_name
llast_name
email
name
username

Actions Editor
Joomla Save User (note that there is a bug that another forum post identified as the "param" field, which was an easy fix. see below)

on success - Save all data cf_table (with all of the above fields saved individually and delete "whole form data" save). In my case, it is Model32.

Created Three New Users on Form 1 (outside of website)

The form works perfect. I created new user1, user2, and user3. All users show up as joomla users on backend, and all can log in.

so far so good.

Create Form 2 for new rigestered users to edit their data.

I copied form 1 and renamed it form 2, and made form 2 for registeredt logged in user permission only.

View editor fields
same as form one plus one new field called userid, (also add userid to database at cf_table)

Actions Editor

Dragged a read data action (Model32) to the "bottom of page 1 in tha Actions Editor..
Inside read data action (, selected r"all matching records" and "populate fields."

For all data fields, add a "value and placeholder like this - "{var:read_data_39.0.Model32.yourfieldnamehereusername}"

e.g., my username field is "{var:read_data_39.0.Model32.username}"

Dragged the save data table action *Model32) out of the on success area of save joomla user action.
Deleted the save jooomla user action.

Open the save data table action, and change setting to update existing record.

Login as New User3 and go to Form 2 as a registered user

I logged in as new user3 to see what user3 sees on zForm 2.

Problem

The good news is that all of the data table info is being read by Form 2

The problem is all users only see row 1 of the cf_table which only has user1 info.

So users 2 and 3 sees the first row of data for user 1. I need a getuser type code so user3 can instead see only user3's own data.

I need user1 to see user1's data row only.
I need user2 to see user2's data row only.
I need user 3 to see user3's data row only.

This answer should help alot of people that need users to be able to efit their own forms data.

Any help is appreciated.


Here is the debug which shows all data table CF-table for user1, user2, and user 3.

Array
(
    [pages] => Array
        (
            [requested] => 1
            [default] => 1
            [this] => 1
            [accepted] => Array
                (
                    [1] => true
                )

            [active] => 1
            [tokens] => Array
                (
                    [1] => b380ec02-5867-4226-8b07-d04725e195b6
                )

        )

    [__lastsave] => 1688772479
    [_data] => Array
        (
            [1] => Array
                (
                    [19dc9363a1f8ded2d8ed70cbf91272be] => 6386fea596e94cff8b6f71b0359fc934
                    [816f405ba48dd0e338f8a49850e9824d] => 5af86fbde046b64b0afe8172f0643eaa
                    [joomla_user_state] => logged_in
                )

        )

    [_vars] => Array
        (
            [1] => Array
                (
                    [read_data_39] => Array
                        (
                            [0] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 9
                                            [first_name] => user
                                            [last_name] => one
                                            [email] => sadad@fgsgs.com
                                            [name] => user1
                                            [username] => userone
                                        )

                                )

                            [1] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 10
                                            [first_name] => user
                                            [last_name] => two
                                            [email] => hfdhdhd@etyey.com
                                            [name] => user2
                                            [username] => usertwo
                                        )

                                )

                            [2] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 11
                                            [first_name] => user
                                            [last_name] => three
                                            [email] => jhjfjd@fhjfhjf.com
                                            [name] => user3
                                            [username] => userthree
                                        )

                                )

                            [3] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 12
                                            [first_name] => user
                                            [last_name] => one
                                            [email] => gggg@dfadfa.com
                                            [name] => user1
                                            [username] => userone
                                        )

                                )

                            [4] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 13
                                            [first_name] => user
                                            [last_name] => two
                                            [email] => jhjfjd@fhjfhjf.com
                                            [name] => user2
                                            [username] => usertwo
                                        )

                                )

                            [5] => Array
                                (
                                    [Model32] => Array
                                        (
                                            [id] => 14
                                            [first_name] => user
                                            [last_name] => three
                                            [email] => dfffs@dfadfa.com
                                            [name] => user3
                                            [username] => userthree
                                        )

                                )

                        )

                )

        )

)

Array
(
    [19dc9363a1f8ded2d8ed70cbf91272be] => 6386fea596e94cff8b6f71b0359fc934
    [816f405ba48dd0e338f8a49850e9824d] => 5af86fbde046b64b0afe8172f0643eaa
    [joomla_user_state] => logged_in
)

Array
(
    [read_data_39] => Array
        (
            [result] => Data read successfully!
            [log] => Array
                (
                    [0] => SELECT `Model32`.`id` AS `Model32.id`, `Model32`.`first_name` AS `Model32.first_name`, `Model32`.`last_name` AS `Model32.last_name`, `Model32`.`email` AS `Model32.email`, `Model32`.`name` AS `Model32.name`, `Model32`.`username` AS `Model32.username` FROM `gur4s_cf_table` AS `Model32`;
                )

            [var] => Array
                (
                    [0] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 9
                                    [first_name] => user
                                    [last_name] => one
                                    [email] => sadad@fgsgs.com
                                    [name] => user1
                                    [username] => userone
                                )

                        )

                    [1] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 10
                                    [first_name] => user
                                    [last_name] => two
                                    [email] => hfdhdhd@etyey.com
                                    [name] => user2
                                    [username] => usertwo
                                )

                        )

                    [2] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 11
                                    [first_name] => user
                                    [last_name] => three
                                    [email] => jhjfjd@fhjfhjf.com
                                    [name] => user3
                                    [username] => userthree
                                )

                        )

                    [3] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 12
                                    [first_name] => user
                                    [last_name] => one
                                    [email] => gggg@dfadfa.com
                                    [name] => user1
                                    [username] => userone
                                )

                        )

                    [4] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 13
                                    [first_name] => user
                                    [last_name] => two
                                    [email] => jhjfjd@fhjfhjf.com
                                    [name] => user2
                                    [username] => usertwo
                                )

                        )

                    [5] => Array
                        (
                            [Model32] => Array
                                (
                                    [id] => 14
                                    [first_name] => user
                                    [last_name] => three
                                    [email] => dfffs@dfadfa.com
                                    [name] => user3
                                    [username] => userthree
                                )

                        )

                )

        )

)


ITadminguy 08 Jul, 2023
Answer
1 Likes
P.S.

Here is the problem with new Joomla User Action, and how I got it working it.

Installed latest CF 7 version in May 2023.

I spent days trying to get the new joomla user to work with the basic form described above. I kep getting this Error about no "params" in the data table. I had no idea what was going on.

The last post here talks about the same "params" error.
https://www.chronoengine.com/forums/topics/view/110218/joomla-registration-form-with-chronoform7

Here is how I solved it, and now the new joomla user action works perfect.

If you go to the joomla users table, there is a column for "params."

If you set up a new user in the joomla backend, it adds the following to the users table in the "params" column.

{"admin_style":"","admin_language":"","language":"","editor":"","timezone":"","a11y_mono":"0","a11y_contrast":"0","a11y_highlight":"0","a11y_font":"0"}


When you drag a save new user into your for in the Actions Editor, after setting up the username, password, etc., go down to the Add Field override.

Add a "params" field.
Then cut and paste this into the value.
{"admin_style":"","admin_language":"","language":"","editor":"","timezone":"","a11y_mono":"0","a11y_contrast":"0","a11y_highlight":"0","a11y_font":"0"}

Save the form

The CF7 form will now create a new joomla user, and the params column for the new user in both the back end and in the users table will have this in it {"admin_style":"","admin_language":"","language":"","editor":"","timezone":"","a11y_mono":"0","a11y_contrast":"0","a11y_highlight":"0","a11y_font":"0"}.

There might be a better way to solve this, but it worked for me.

Max_admin 08 Jul, 2023
Hello itadminguy,

Thank you for posting the solution.

All you need to add in the "params" field override is actually "{}", that's all, that should make it work.



This issue should be solved in the 7.0.11 update
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ITadminguy 08 Jul, 2023
sure.

Note, CF7 is way better than CF5. The overall use is much easier, particularly for each field.

The only problem I have is noted above.

For CF5, Ithis is what I did to get the user that created a table row to be able to edit and re-save the data.

I need this to work in CF7 to make it useful.

In CF5, My Form 1 would add new fields and then save all data to the EASY Profile Fields, including extra fields I created and was able to save via Easy Profile for each user in their user profiles in core joomla, and form 2 would "recall that data" from core joomla user profile (and not from Form 1 that created all that data) so it could be used and edited.

So in CF5, My Form 2 would recall all that data from the user profile data table. The only problem now is that Easy Profile does not exist for J4. So Instead I want CF7 Form 2 to just recall the data from CF7 Form 1 and not from the core joomla user profile fields that CF5 Form 1 created and added to core joomla user profile..

***********
All f this was CF5.
On Load
DB Read Action and select the table from phpmyadmin for this CF5 Form 2 that I am trying to edit/
Put this in the DB Read conditions box

<?php 
    $user_id = \JFactory::getUser()->id;
    if ($user_id != 0) {
          return array("user_id" => $user_id);
    }
    ?>


On Load
custom code
<?php
 $user =& JFactory::getUser();
$user=JsnHelper::getUser();
$form->data['user_id'] = $user->id;
?>


On Load
CUSTOM CODE
REQUIRE ONCE (this was a small snippet of php code that was needed)

Followed by

On Load
DB Read JSNUSERS
DB Read JSNFIELDS

<?php
 $user = \JFactory::getUser();
 $form->data['user_id'] = $user->user_id;
$form->data['username'] = $user->username;
$form->data['email'] = $user->email;

(etc... for all of the other Easy Rpofile user fields I wanted the user or admin to recakk into form 2, edit and resave, where that data was originally saved byForm 1 in CF5 form1. so that Form 2 could now accomplish something else with the saved data - e.g., to populate the fields in a contract.)
}

On Load
DB Read Table
Conditions box
<?php
    return array("user_id" => $form->data["user_id"]);
    ?>


***************

I found this from a different post.
https://www.chronoengine.com/forums/topics/view/109555/php-get-user-id-after-user-registration
"OK....one solution that works is this"
​$db = JFactory::getDBO();
$db->setQuery('SELECT id FROM #__users WHERE username = ' . $this->data("username"));
$user_id = $db->loadResult();


Will ths get the user and the user data in CF7 Form 2 from the cf-table created by CF7 Form 1?

ITadminguy 15 Jul, 2023
Hoping that someone knows how to read the CF7 and J4 data table inputted by a specific user via the CF7 form that just registers them as a new user, instead of each user only being able to read the first row in that data table.

Thanks in advance

Max_admin 15 Jul, 2023
if the problem is reading the record of a specific user then your can use the "Where conditions" in the "Read data" action to do that, in the conditions add this:
user_id = {user:id}


Do you need to read the record of a the current logged in user ? or did I misunderstand you ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ITadminguy 16 Jul, 2023
Good news and bad news.

Good news is I got it working.

Bad news i that I have to manually enter the user_id by editing the "user_id" column inphpmyadmin in my cf_table.

The basic problem is that I cannot get CF7 to insert the user_id into a form when that form registers the user for the first time. If I insert it by hand in phpmyadmin for the "user" that created the table created just for the CF7 form (where that sane form also created their user id/profile in the users table), then this user_id = {user:id} will call the data up in the CF7 form for the user cid that created the auto increment "id" of the form.

Example. Basic contact form. Also has a joomla register new user action. Followed by a save table action.

Fields are id (hidden), uer_id (hidden), name (input or let joomla new user action auto create it), firstname (must input), lastname (must unput), username (input or let joomla new user action auto create it), email (must input), and favorite_color (must input).

In the Actions area of the Basic Contact Form, there is a Create Joomla User Action. If success, then there is a save to action to a new table which I called cf_table with all fields input as columns.

New user fills it out. The data is saved to the cf_table. The new user is created in the user table.

Example. Lets say the 10th person to ever fill the form out has this saved in the cf_table.

Results are -

in the cf_table the id is 10 (auto assigned by the form), uer_id is "0" (nbecause o user id is assigned by the form into the cf_table or by the joomla create enew user action), firstname is Joe, lastname is Smith, username is JoeSmith, email is joe@smith.com, and favorite_color is red.

In the users table, let's say Joe is user 180.

Joe logs in or admin logs in as joe to change his color from red to blue.

The problem is that the CF7 form in which just created the user oes not have a user_id in the table (which I called cf_table) to know that joes is user 180 and that he was the 10th person to create an id in the basic contact form in the cf_table.

I narrowed the problem down. When Joe filled out the basic contact form, the first jpomla user action created his new joomla user id. If success, the basic contact form was saved. So far so good.

But upon success, I couldn't find a way to ALSO get the new ly created user id (180 in Joes case) cf_into the tcf_able of the basic contact form under "user_id" by saving in the Savev data to cf_table action.

Remember, in the users table, the user has a "id" column, and not a "user_id" column.

So the cf_table has ab "id" column as the auto increment for each person that fills out the basic contact form. I created another column as "user_id" which I tried to automatically stuff the new joomla user id into as soon as it was created. No luck. I have o input it by hand.

I tried everything. I tried to modify the in the basic contact form (I tried to save joomla user, then read user table, then save to cf_table), and even in the second registered users only basic contact form (I tried to read both cf_table and users table to try to put joes id of 180 into the user_id in the cf_table). I tried in vain to get the newly created user "id" into the "user_id" of the cf_Table. Basically, when Joe filled out the basic contact form, it knew that it was Joe and his favorite color, but it did not know what his new joomla user id number was.

[I think this problem is only for forms on the outside of the website that create new users.
[Once someone is a user, I suspect that a registered form can be created which will save some completely different form with the user id also beinng saved. I could do it in CF5 and J3, and will be trying here shortly in Cf7 and J4.]]

Why is it important to change the color from red to blue? In my case, I have an application The user only has to input name, phone, and email, and can fill out some, none, or all other questions. There are about 10 other questions. If I call the user back to fill out the rest of the form, I can log in as the user and do it for them, and it creates a pdf application with all the data needed. PTheir data from the initial form has to populate.

So the way to do this to "edit" joes form (via the second basic contact form for registered users only) is to manually look up and input his new user id into the "user_id" column of cf_table in phpmyadmin.

Joe logs in. All of Joes data from line 10 of the cf_table, which now also includes the "user_id"hich I inputtedby hand, populates this second form. Change red to blue. Press send. And blue is saved.

The joomla reg action was deleted frorm form 2, and the read data and the save data to cf_table action are the only action sin form 2.

Here is the simplest way to edit the basic contact form. (note that you can also read from both the cf_table and the user table to make sure the user data if you want it to be edited in the cf_table gets simultaneously updated to current info in the user table).

Form 2 (Basic Contact Form Reg Users Only) -
The value and placeholder for each field has

id field (no longer hidden but make read only) {var:read_data_39.0.Model32.id} (replace 38 with the number of your read data action).

user_id field (no longer hidden, bbut make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.user_id}

name field (no longer hidden, but make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.name} (note that core joomla has a "name" field only and not firstname and last name fields)

username field (no longer hidden, but make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.username}

firstname field (no longer hidden, but make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.firstname}

lastname field (no longer hidden, but make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.lastname}

email field (no longer hidden, but make read only if you dont want user details saved in user table){var:read_data_39.0.Model32.email}

favorite_color field (var:read_data_39.0.Model32.favorite_color}

Read data action on page 1
read cf_table all matching records where condition user_id = {user:id} Make it the same Model# from the Model# in basic contact (public) form save data
Populate fields

Save data action on page 2 Make it the same Model# from the Model# in basic contact form (public) save data action.
Add the individual data fields that need to be saved.
Uncheck whole form data.

(note, or you could also make the username, name, remail, etc editable and create a second Save action to the core joomla users table to update those at the same time if they are edtable and not read only in the second basic ontact form fornreg users only).

Max_admin 16 Jul, 2023

The problem is that the CF7 form in which just created the user oes not have a user_id in the table (which I called cf_table) to know that joes is user 180 and that he was the 10th person to create an id in the basic contact form in the cf_table.


You can get the id after the "Save User" action runs, just enable the form debug and check the debug results, the id will be there, you need to call it using:
{var:save_user_action_name.id}
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.

VPS & Email Hosting 20% discount
hostinger