Problem accessing user data

austega 07 Oct, 2010
I have successfully developed:

1. A CF form where a logged in user can submit a research interest
2. A CC list so that a registered user can see the research interests they have submitted with a delete and edit option
3. An edit CF form for the above CC list which uses the Profile Plugin (and was the subject of another forum post http://www.chronoengine.com/forums.html?cont=posts&f=5&t=19494 [/list]

The final step of this little app is for any user (ie not logged in) to search all the submitted research interests, with search or filter functionality, and be able to submit an inquiry in regard to one of the results. So far I have successfully created:

4. A CC filterable/searchable list of research interests http://test2.sag.org.au/component/chronoconnectivity/?connectionname=Srch_ResearchInterests
5. An inquire link on each of the above that uses the CC Edit_Record code to call up a CF form that lists the selected research interest record and captures the inquirer's detail
6. This CF form tries to send two emails, one confirmatory one to the inquirer which works well, and the second one submits the inquiry to the registered user that submitted the research interest - and this is where I have a residual problem![/list]

The only user information attached to the research interest record is the joomla user id which is stored in a cf_user_id field.

I have the following hidden fields in the form html field which seems to successfully bring in the information from the appropriate CC record.

<input type='hidden' name='cf_user_id' value={cf_user_id} />
<input type="hidden" name='rsch_name' value={rsch_name} />
<input type="hidden" name='location' value={location} />
<input type="hidden" name='period' value={period} />
<input type="hidden" name='notes' value={notes} />


I currently have the following code in this form's On Submit field:

<?php
if ( !$mainframe->isSite() ) { return; }
$membid=$_POST['cf_user_id'];
$db =& JFactory::getDBO();
$query = "SELECT * FROM #__users WHERE id = ".$membid.";" ;
$db->setQuery($query);
$result = $db->loadObject();
JRequest::setVar('username', $row->username);
JRequest::setVar('email', $row->email);
JRequest::setVar('name', $row->name);
?>


but started with

<?php
if ( !$mainframe->isSite() ) { return; }
$db =& JFactory::getDBO();
$query = "SELECT * FROM #__users WHERE id = 'cf_user_id' ";
$db->setQuery($query);
$memb = $db->loadObject();
JRequest::setVar('username', $memb->username);
JRequest::setVar('email', $memb->email);
JRequest::setVar('name', $memb->name);
?>


and have tried a lot of variations in between reflecting my steep learning curve re the coding required for SQL and PHP. I suspect that this remains where my problem is.

The page gives an error Could not instantiate the mail function. One of the emails is sent and received perfectly. The other works when I replace the 'cf_user_id' in the above code with an appropriate user no, but otherwise I have not been able to pick up the associated user's name or email address (the latter used in the Dynamic To field).

I had a good look at http://docs.joomla.org/How_to_use_the_database_classes_in_your_script but nonetheless think the above code segment is my problem.

Any assistance gratefully appreciated.
GreyHead 07 Oct, 2010
Hi austega,

From a quick look your code looks OK to me. However, you don't need to use a database query to get user information. Joomla! has some User Object methods that shortcut some of this.

You can get the info on the current user with:
<?php
$user =& JFactory::getUser();
echo $user->email;
. . .
?>

You can also use this to get the info on any other user with a small change
<?php
$membid =& JFactory::getInt('cf_user_id', '', 'post');
$memb =& JFactory::getUser($membid->id);
JRequest::setVar('email', $memb->email);
?>


You then need to debug the email problem. The first step is to turn Debug on in the form General Tab and carefully check the resulting output when you submit the form - particularly the addresses in the 'dummy' emails.

Bob
austega 07 Oct, 2010
Thanks Bob. I was amazed that a quick scan did not reveal some major coding errors.

In any case I liked and tried your suggested use of the User object, and in particular now have the following code in the On Submit field:

<?php
if ( !$mainframe->isSite() ) { return; }
$membid =& JFactory::getInt('cf_user_id', '', 'post');
$memb =& JFactory::getUser($membid->id);
JRequest::setVar('username', $memb->username);
JRequest::setVar('email', $memb->email);
JRequest::setVar('name', $memb->name);
?>


However this now returns this error:

Fatal error: Call to undefined method JFactory::getint() in /home/sagorg/public_html/test2/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 3

I also turned debug on again - I had turned it off when it did not seem to generate a debug trail. I had assumed this was because the form was being called from the Edit Record code in the CC data connection - and somehow this precluded the debug trail.

Could this CC Edit-Record call be also causing the above error?

Thanks for your help - David
GreyHead 08 Oct, 2010
Hi austega,

My mistake it should be JRequest::getInt( . . . not JFactory::getInt( . . .

Bob
austega 08 Oct, 2010
Thanks Greyhead,

If you have any good link on these JRequest and JFActory constructs I would appreciate it.

I know have
<?php
if ( !$mainframe->isSite() ) { return; }
$membid =& JRequest::getInt('cf_user_id', '', 'post');
$memb =& JFactory::getUser($membid->id);
JRequest::setVar('username', $memb->username);
JRequest::setVar('email', $memb->email);
JRequest::setVar('name', $memb->name);
?>


in the On Submit (before emails) field, and no longer receive the fatal error - but rather a notice: "Notice * You must provide at least one recipient e-mail address." and the second email that has a dynamic to address of "email" without the quotes is not being sent. It appears that no values for username, email and name are being read - while the cf_user_id variable is showing the correct value.

I also tried moving the code into the main form html field with no apparent difference.

What am I doing wrong? Could the CC calling of this form be the cause?
GreyHead 08 Oct, 2010
Hi austega,

Not one of my better posts :-(
$memb =& JFactory::getUser($membid->id);
should be
$memb =& JFactory::getUser($membid);


You can use the ChronoSearch link from the Tutorials menu above to find the JFactory & JRequest docs. ChronoSearch is a custom Google search for this site, my site at greyhead.net and the Joomla! docs wiki.

Bob
austega 08 Oct, 2010
You will do me, Bob. App is now working as it should and I have climbed the learning curve a little higher.

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