Forums

How to populate form fields with data from CB?

dtcinc 28 Jan, 2009
I have searched the forum and have read many, many posts regarding this issue. But I have not yet found a post that stays on track with this topic or one that has the complete solution. Many posts jump around between posters or are incomplete. If there is a COMPLETE A to Z on what I am trying to accomplish please post it here. It would be great if there was a tutorial or a backup form.

What I am trying to do:
Users register and login using CB 1.2. If the user requests information by snail mail, they click on a link to the form. I would like chronoforms to pull information from their profile and populate form fields based on data in the user profile. I don't need to write data back to the user profile.

I have tried many things, but I am lost.

Thanks,
Wes
GreyHead 28 Jan, 2009
Hi Wes,

There is no tutorial to do this, and neither Max or I are frequent users of CB.

The trickiest bit is going to be finding where CB stores the data, once you know that you can write MySQL query to read it and set the values in the Form HTML.

Bob
Max_admin 28 Jan, 2009
Hi Wes,

As bob said, if you can find the CB table name which has the users data then you can use the Chronoforms "profile plugin" to load the data at your form, this plugin can load the data from ANY table in the database!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
dtcinc 28 Jan, 2009
Bob & Max,

The CB table name that holds the data I need is called jos_comprofiler.

jos_comprofiler fields (that I need):
id
user_id
firstname
middlename
lastname
company
address
city
state
zipcode
country
phone

Please let me know what the next step is.

Thanks
cjmicro 29 Jan, 2009
The steps are to #1 in the forms management, check the form you want to enable this for, then click "profile plugin" (well first you have to d/l and install the plugin). (I should specify this is using cf 2.3.9 so not sure what changes in cf 3)

Then You select your jos_comprofiler table, and the unique id (I use user_id)

Then you have to enable the plugin in the form admin on the plugins tab.

Then you create your form and substitue {fieldname} where you want the value to go, here is a working example off one of my forms.

<table cellpadding="5" cellspacing="0" border="0" width="100%" id="basa" bgcolor="#E5E5E5" style="border-collapse: collapse">
    <tr>		
	<td colspan="2" height="45" width="853">
<h3 align="center">Registrant Information</h3>

<p style="margin-left: 40; margin-right: 40">If you are a PTO Member and do not see your information in the text boxes below, please login with your user name and password in the header above.  Your information should appear automatically. </p>
</td>
</tr>
<tr>
			<td align="right" width="178">First Name:*</td>
			<td align="left" width="665"><input type="text" size="40" id="firstname" name="firstname" value="{firstname}" /></td>
		</tr>
  <tr>
			<td align="right" width="178">Last Name:*</td>
			<td align="left" width="665"><input type="text" size="40" name="lastname" id="lastname" value="{lastname}" /></td>
		</tr>
<tr>
		<td align="right" width="178">Main E-mail:*</td>
		<td align="left" width="665"><input type="text" id="email" name="email" size="40" value="<?php global $my; echo $my->email; ?>"    /></td>
	</tr>
    		....yadda yadda
Hope this helps.

Cheryl
dtcinc 29 Jan, 2009
Thank you Cheryl. I remember seeing one of your posts before regarding the CB connection. I was making some simple mistakes before regarding which plugin to use. I will add a post to contain a full step by step on how to do this, but have a few more questions first.

1 > CB doesn't include the email field in the jos_comprofiler table, it is in user jos_users (at least for me). How do I access the email field in jos_users? What are you doing different? I copied the code from your sample for email, but it is doesn't populate. I am guessing this is because it's in a different table. The fields in jos_comprofiler work fine.

2 > Did you find a way to clear field values if the user was not logged in? In a perfect world, a non-registered user would see a blank form without {firstname} {lastname} in the field values.

Thanks.
cjmicro 29 Jan, 2009
I definitely recall seeing a message where there was a script to tell if the person was logged in or not to include the curly bracket values or not. You can probably do a search to find it, but I have not implemented it. Has something to do with if myid--> or somethng like that.
<?php if ($my->id) echo "{fieldname}" ?>
at the top something like $my = JFactory::getUser();

Check this topic:
http://www.chronoengine.com/forums.html?cont=posts&f=2&t=10718&st=0&sk=t&sd=a&hilit=profile+plugin&start=15

Also, the global value for email that I use is for joomla 1.0 and its probably different if you are on joomla 1.5. Something like this... but I don't know exactly how to program it.

<?php
$my = JFactory::getUser();
echo $my->email;
?>

Check this out, maybe it will help
http://www.latenight-coding.com/joomla-faqs/developers/get-logged-in-user-info.html

I'm sorry I don't have exact answers, but maybe this can help you in the right direction. If you get it working so the curly brackets don't show up can you share? I have to figure out how to get tabs to go to the top of the page, that's my next mission. :-)

Cheryl
dtcinc 26 Feb, 2009
okay, so I had a little more time to hack away at this. I would like form fields to be blank for guests.

There has got to be about 100 better ways to do it than this:
<?php
$my = JFactory::getUser();
?>
<?php if ($my->id){ ?>
<div class="form_item"><div class="form_element cf_textbox"><label class="cf_label">First Name</label><input class="cf_inputbox" maxlength="150" size="30" id="firstname" name="firstname" value="{firstname}" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_textbox"><label class="cf_label">Last Name</label><input class="cf_inputbox" maxlength="150" size="30" id="lastname" name="lastname" value="{lastname}" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_button"><input value="Submit" name="undefined" type="submit"></div><div class="clear"> </div></div>
<?php } else { ?>
<div class="form_item"><div class="form_element cf_textbox"><label class="cf_label">First Name</label><input class="cf_inputbox" maxlength="150" size="30" id="firstname" name="firstname" value="" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_textbox"><label class="cf_label">Last Name</label><input class="cf_inputbox" maxlength="150" size="30" id="lastname" name="lastname" value="" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_button"><input value="Submit" name="undefined" type="submit"></div><div class="clear"> </div></div>
<?php } ?>

Suggestions? Thanks.
Max_admin 27 Feb, 2009
Hi dtcinc,

easy answer is the "profile" plugin for Chronoforms, please search the forums for how to use it with many examples!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
cjmicro 27 Feb, 2009
I think he means how to get it not to show the field values {fieldname} in the fields where people are not logged in/registered.

Cheryl
dtcinc 27 Feb, 2009
Yes Cheryl, that's right. Guests should see a blank form, users see their profile info.

I think this is what I am going with
<?php
$my = JFactory::getUser();
?>
<div class="form_item"><div class="form_element cf_textbox"><label class="cf_label">First Name</label><input class="cf_inputbox" maxlength="150" size="30" id="firstname" name="firstname" value="<?php if ($my->id) echo "{firstname}" ?>" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_textbox"><label class="cf_label">Last Name</label><input class="cf_inputbox" maxlength="150" size="30" id="lastname" name="lastname" value="<?php if ($my->id) echo "{lastname}" ?>" type="text"></div><div class="clear"> </div></div><div  class="form_item"><div class="form_element cf_button"><input value="Submit" name="undefined" type="submit"></div><div class="clear"> </div></div>


using value="<?php if ($my->id) echo "{firstname}" ?>" for the field values

Thanks for the help
GreyHead 27 Feb, 2009
Hi dtcinc,

Out of curiosity what does a guest see if you just use value='{firstname}' ?

Bob
dtcinc 27 Feb, 2009
{firstname} as the field value
GreyHead 27 Feb, 2009
Hi dtcinc,

Thanks, when I have a moment I'll see if I can find a quick fix.

Bob
cjmicro 27 Feb, 2009
So with dtcinc's version, you have to put the code in for every field value, not just once at the top or something, right?
Cheryl
dtcinc 27 Feb, 2009
right, you need the php code at the top to get check for a user, and the other small php code in each value to echo the user values if they exist. If it's not a user, the field value will be blank.

Let's see what Bob comes up with, might be easier to manage.
Max_admin 27 Feb, 2009
aha, I will add an updated version of the profile plugin soon which will fix this issue!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
cjmicro 27 Feb, 2009
Awesome Max, that will be great!! It's kind of annoying seeing the fieldnames in there, although it's great to be able to get them at all!! Other components who shall remain nameless...ahem<coughcough>, do not have such a plugin and it is INFINITELY useful.

Thank you!!

Cheryl😀
GreyHead 28 Feb, 2009
Hi Cheryl,

Here's a quick attempt at this. Keep a backup copy of the original cf_profile.php then replace the onload() function - rougly lines 155-191 with this code (NB there's one more } after it!!)
	function onload( $option, $params, $html_string ) {
		global $mainframe;
		$my 		= JFactory::getUser();
		$database =& JFactory::getDBO();
	
		$parid 	= JRequest::getVar( $params->parameter, '', 'request', 'int', 0 );
		if ( $parid ) {
			$record_id = $parid;
		} else {
			$record_id = $my->id;
			if ( $record_id == 0 ) {
			    $record_id = '##guest##';
			}
		}
		
		if ( $record_id == '##guest##' ) {
			$result = $database->getTableFields( '#__users' );
			$table_fields = array_keys($result[0]);
			foreach($table_fields as $table_field){
				$html_string = str_replace("{".$table_field."}", '', $html_string);
			}
		} elseif ( $record_id ) {
			$database->setQuery( "
				SELECT * 
					FROM ".$params->table_name." 
					WHERE ".$params->field_name." = '".$record_id."'" );
			$rows = $database->loadObjectList();
			$row = $rows[0];
			$tables = array( $params->table_name );
			$result = $database->getTableFields( $tables );
			$table_fields = array_keys($result[$params->table_name]);
			foreach($table_fields as $table_field){
				$html_string = str_replace("{".$table_field."}", $row->$table_field, $html_string);
			}
		}
		return $html_string ;
	}
Not tested and will need debugging! This will only work (if it works at all) with the jos_user table for guest with ids of 0.

Bob

Later: edited to add a missing }
This topic is locked and no more replies can be posted.