Forums

Auto Loading logged in username on form and submitting value once.

foluso 11 Mar, 2017
Hello @ all,

I have created a form that can only be accessed when logged in.
I have also created a table that stores the values of the form.

Now I want to load the username of a logged in user in one of my form fields....
When a user submits form, the username of the logged in user immediately gets submitted to the table with other form details.

Finally i want to restrict the user to be able to submit once.

Your kind suggestions will be appreciated.
GreyHead 11 Mar, 2017
Hi Foluso,

Please see this FAQ - though better to save the user iD than the name, you can always look up the name from the ID

To limit access use a DB Read or a Custom Code action in the On Load event to check if there is already a record saved, then an Event Switcher to handle the response i.e. show a message and stop, or show the form.

Bob
foluso 12 Mar, 2017
Hello,

Thank you for the suggestion...
I made use of custom code, inserting the codes below, since i want to verify against the logged in user's username.
<?php
$user = \JFactory::getUser();
$form->data['user_name'] = $user->name;
?>

I also configure the event switcher based on the information at this link:
https://www.chronoengine.com/faqs/70-cfv5/5212-event-switcher.html

I later inserted the code below in the event switcher
<?php
if ( $form->data['user_name'] == 'user_name' ) {
  return 'success';
} else {
  return 'fail';
}
?>

I left the default value "success,fail" in the Events Field.

I believe on loading the the form when logged in, a success message should display, but its not so... I must be doing something wrong...

What i intend to achieve is to check that the username of the logged in user and display it in a form, then submit the information alongside the form.

Your kind suggestions will be appreciated.
Regards.
GreyHead 12 Mar, 2017
Hi Foluso,

Did you add any actions to the On Success and On Fail events of the Event Switcher? If they are empty then nothing will happen.

Bob
foluso 13 Mar, 2017
Hello,

Thank you for the suggestion..
I dont want to get it all complicated, first I will like to ascertain that the username displays in the form, for that i have added the codes below in the custom code in the onload Event

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

But it will not display the username when the form loads, now i added a debugger, the error message gotten is below;

Errors
Array
(
)

I think the above should work first before i add the event switcher to limit submission.

Kindly advice on how to eliminate the errors so the username can display in the form

Thank you for your suggestions.
GreyHead 13 Mar, 2017
Hi foluso ,

The empty Errors array tells you that there are no errors.

Have you added the Custom Code action *before* the HTML (render form) action? Otherwise it will not have been run when the form HTML is created.

Bob
foluso 13 Mar, 2017
Yes I have, its before the render form action(i have attached screenshot)...

Its still not showing on the form...Please what should i do.

Thank you
GreyHead 13 Mar, 2017
Hi foluso ,

Then I guess that you don't have anything in the Designer tab to display the username. A readonly Text box element will do that and allow it to submit with the form data.

Bob
foluso 13 Mar, 2017
Hello,

Thank you for your support... So far i have been able to display the username successfully in the form... Thank you very much.

By reading up this link https://www.chronoengine.com/forums/posts/t516/how-to-insert-logged-in-user-details-like-username.html?keywords=logged%20in%20username%20

I created the readonly text box and inserted the code below which now works like WOW!

<input name="fieldname" id="fieldname" type="text" readonly="readonly" value="<?php
$user = \JFactory::getUser();
$form->data['user_username'] = $user->username;
echo $user->username;

?>" />


Now am moving on to limiting the submission per user, trying out the Event switcher and Db Read... I will keep you posted on new developments...

Warm Regards...
foluso 16 Mar, 2017
Hi,

I have been able to Limit user submission using the DBread....

Finally am now trying to verify and register users that are not logged in, the idea is simple...

I have created a seperate database of users with names, phone number etc...
I have also created the verification form, which has the joomla registration fields(username, name, email address and password).

I want the form to check the database column with usernames, upon submission to see if the record exist in the database then allow the joomla registration continue if not redirect users to a page or display a message.


Thanks in advance for your kind suggestions.

Warm Regards.
Max_admin 17 Mar, 2017
Hi foluso,

If you add a "db read" action and use the code below in the "conditions" box:

<?php
$user = \JFactory::getUser();
return array("username" => $user->username);
?>

Then you can run the "save" action in the "on not found" event, that's when the username does not exist in the table.

Best regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
foluso 17 Mar, 2017
Hello admin

Thanks for your reply....

I have tried the code above but its not working as expected maybe, i didnt explain my self properly enough the first time...Let me explain what i intend to achieve once more...thanks

I have created a separate table in my database(member table), one of the fields(username) holds the data i want to check on submitting the registration form(I created a joomla registration form in chrono).

The form is available to public since the purpose is to register new users, therefore In this case the user is not yet logged in.

On submitting i want the form to check the username submitted against the usernames uploaded in the new table, this is to verify that the user registering is a member, that's why i created a new table to hold members information.

The form i intend to create is a joomla registration form, that only allows usernames listed on the separate table to register on the website.

I hope you have gotten a clearer picture now....Thank you.

Regards
GreyHead 17 Mar, 2017
Answer
Hi foluso ,

If you want to check the newly submitted username then the code would be
<?php
return array("username" => $form->data['username']);
?>

Bob
foluso 17 Mar, 2017
Hello GreyHead,

Thanks for your response...
I tried the code above but still the registration wont work...Let me explain what have done so far..

I have a created and uploaded a table that holds the members information.

I have created the form with the following fields fullname, username, email, password and confirm password..just like joomla registration

In the setup i have added the DBread Action with the code below

<?php
return array("username" => $form->data['username']);
?>

I set the DBread Table name to the newly created database table.

In the onfound action of the DB read I have added the Joomla registration action.
I have set all the parameters for the joomla registration to work effectively. (this should take effect once the username is found)

In the onsuccess i added email actions
In the onfail I added event loop

In the Dbread On not found i added a display message "not found".

when i submit the form using a username that already exist in the new table, instead of the onfound action of the DBread taking effect the On not found takes effect...why???

This simply means the Dbread is not reading the data... or i might be missing something else...

Kindly assist.
Thank you in advance for your suggestions.

Warm Regards.
GreyHead 17 Mar, 2017
Hi foluso,

I suggest that you add a Debugger action so that you can see the query that is being created. That should help you pin down exactly where the problem is.

Bob
foluso 17 Mar, 2017
1 Likes
You guys are the best!!!😀😀😀

I see whats wrong it seems i am currently running on custom code, there are some field names that did not update upon saving which causes the conflict....

The code below now works like magic....**winks**....smiles

<?php
return array("username" => $form->data['username']);
?>

Am really grateful for your support...

Best regards.
This topic is locked and no more replies can be posted.