Forums

Loading no user details in form

spletcher 17 Feb, 2013
I've followed how to add the userid to the DB Record Loader and gotten it to work using this method:

http://www.chronoengine.com/faqs/2674-how-can-i-edit-a-record-from-a-database-table.html

I put user_id in the request parameter for the DB Record Loader, but then if a non-logged in user fills out the form, since there is a "0" for his ID placed in the form, this data is loaded each time the form is pulled up and no-one is logged in.

How do I prevent having this happen?

Thanks,

Stan
GreyHead 17 Feb, 2013
Hi Stan,

I'd probably use a Custom Code action in the On Load event to check the user id and remove any pre-loaded data if it's a guest.

Bob
spletcher 17 Feb, 2013
Bob,

This is what I have currently in my custom code just before the DB Record Loader:
<?php
$user =& JFactory::getUser();
$form->data['name'] = $user->name;
$form->data['email'] = $user->email;
$form->data['user_id'] = $user->id;
?>

How do I remove any pre-loaded data? I'm assuming some statement that says if user_id = 0 then do something.
GreyHead 17 Feb, 2013
Hi Stan,

Something like this?
<?php
$user =& JFactory::getUser();
if ( $user->id === 0 ) {
  $form->data['name'] = '';
  $form->data['email'] = '';
  $form->data['user_id'] = '';
} else {
  $form->data['name'] = $user->name;
  $form->data['email'] = $user->email;
  $form->data['user_id'] = $user->id;
} 
?>

Bob
spletcher 17 Feb, 2013
Bob,

OK - I've tried that code in the custom and data still populates from the record with a userid of 0. Not sure why.

Is there a place to put some code within the DB Record Loader to prevent it from loading the data?

Stan
spletcher 18 Feb, 2013
Bob -

I have resolved this issue. For everyone's benefit reading this string.

If I put this code in below in a custom code event, followed by a DB record loader with the parameter set to user_id, it still populated my form:

$user =& JFactory::getUser();
$form->data['user_id'] = '';

So the answer was the following code which did not populate the form for non-logged in users:

<?php
$user =& JFactory::getUser();
if ( $user->id == 0 ) {
$form->data['name'] = '';
$form->data['email'] = '';

} else {
$form->data['name'] = $user->name;
$form->data['email'] = $user->email;
$form->data['user_id'] = $user->id;
}
?>

I'm not quite sure why I had to take out this line: $form->data['user_id'] = ''; under the if statement, but it certainly causes the form to populate with the table data where the record is "0"
Incidently, having this code: $form->data['user_id'] = '1'; also works for me as well. If anyone has a good explanation for this, it would be interesting although not important for solving this specific issue now.
GreyHead 18 Feb, 2013
Hi Stan,

Well solved. I'm not sure why that line would make a difference, it may be related to that way that ChronoForms checks for values before using defaults when it saves the data.

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