Forums

Lost in how to connect a user registration to a user detail

Laranjinho 12 Nov, 2009
Hi,
I'm struggling a while now to get chronoforms to work like I need, but I am out of time and really need a solution.
1) I managed to make a user registration form which replaces the standard Joomla! registration form; this works fine, and date is written correctly in the 2 tables (standard and the chrono made).
2) I need in a user menu (logged in user) a link to let him update his details, I know I have to do something with a new form and profile page, but I am getting lost there. I have no clue how to acchieve this.
3) In the registration form I added a field, which is a "motive" why the user is registered. I need somehow to manage that when a user has motive "teacher" he will automaticly get in his user menu the correct link to a form for more details about him. If his motive is "client" he should get another form with some other details.
4) When I make a menu item to call the data stored in the chrono database (database shows data correctly), it appears in my components list, but when I click it is says "Table Doesn't Exist" and nothing more.

Any help will be deeply appreciated!!!
GreyHead 12 Nov, 2009
Hi Laranjinho,

Sorry I don't have time to answer this now and I'm going to be away for a few days, Hopefully Max or Fredrik will pick this up.

Bob
nml375 13 Nov, 2009
Hi Laranjinho,
Lets see what we can do here..

1, That sounds good. We'll build from that.

2, I'd suggest you create a second form, linking it to a "registered"-access menuitem. Further, you'll need some custom php-code in your form (you could use the profile-plugin, although I'd recommend not using it for this. Especially since the profile-plugin loads a record based on a user-submitted value, rather than the id of the currently logged in user).
Even further more, you'll need the same DB Connection as in your registration form (although you don't need the user registration plugin here), as this will do the DB updates for us.

<?
$user =& JFactory::getUser();
if ($user->id == 0)
{
//User not logged in, stop loading the form...
  echo "You are not allowed to access this page. Please login first!";
  return;
}

//Get an instance of the JDatabase object...
$db =& JFactory::getDBO();

//Build the SQL-query...
$query = sprintf('SELECT * FROM %s WHERE %s = %d LIMIT 1',
  $db->nameQuote('jos_userdata'),
  $db->nameQuote('cf_user_id'),
  $user->id
);
//... load it, and get the result..
$db->setQuery($query);
$userData = $db->loadObject();

//Get the session storage, we'll need this to keep track of which database record we're editing:
$sess =& JFactory::getSession();
$sess->set('cf_id', $userData->cf_id, md5('chrono'));

//Now we've got all the data, proceed to the actual form:
?>
<input type="text" name="text_1" value="<? echo $userData->text_1; ?>" /><br />
<input type="text" name="text_2" value="<? echo $userData->text_2; ?>" /><br />
....


Now, the DB Connection should handle the database save, however, unless we intervene, it'll just create a new record rather than update the old one. As such, we'll need a few extra lines of code to tell it where to store the data:
<?
$sess = JFactory::getSession();
$cf_id =& $sess->get('cf_id', 0, md5('chrono'));
JRequest::setVar('cf_id', $cf_id);
?>

For this to work most smoothly, I'd suggest you enable emails on the general tab (regardless whether you use any emails or not). This allows us to use the "on submit - before email" box for the above code, which will always be executed before the autogenerated code (aka DB Storage).

This will only handle the data that is stored in the chronoforms' made database. If you wish to load/update data from the Joomla user database, you'll need some additional code.

3, This makes things alittle more difficult. I'm scratching my head on this one at the moment. I don't see any ways of creating a dynamic menu item, as the menu's are beyond chronoform's control. You could create a third form however that is available to all users. This form would then retrieve the custom information and check the 'motive' value, and then render the appropriate form accordingly. The downside of doing it like this however, is that you will most likely not be able to use the DB Connection feature in this form, but have to do all database storages with your own code.

4, I'm not sure on this one. There seems to be a bug, as far as I can read from the source, there's an additional parameter in the url that's needed, but not added, being "table" holding the actual tablename. You could try manually editing the jos_components db-table, but be warned - making a mistake here could prevent your Joomla administrator interface from working. Locate the menu link you added, and add the &table=tablename to the link and admin_menu_link values.

/Fredrik
Laranjinho 16 Nov, 2009
Hi Fredrik,

Thank for your reply, I really appreciate your help 😀
2) I am stuck here...I copied my regustration form and tried enter your php cone in the formcode tab, under HTML. But this does not deliver the desired result, I am getting an empty form with equal additional fields without name to fill in 😶 I think I do something mega wrong here.

3) I think it is best to just add a 2nd form with optional data for all groups, too complexe to get this done now and it looks to me that adding an extra form is the easiest way....

4) Not tried out yet.....

regards
nml375 16 Nov, 2009
Hi Laranjinho,

Well, the code I posted would have to be merged with your form, rather than simply added. I guess I could've been a bit more clear on that in my previous post.
The part up until "//Now we've got all the data, proceed to the actual form:" is pretty much straightforward, you'll have to change "jos_userdata" with whatever you named the "chrono-made" one.

The rest of that code illustrates how to insert the value from the database into your form. In the case of your "motive" field, the code should look like this:
<input type="text" name="motive" value="<? echo $userData->motive; ?>" />


For the "on submit - before email" part, that should work without any modifications.

/Fredrik
comraddan 09 Dec, 2009
Thanks for this code it works great! I had a quick question, could you also give an example how to repopulate a checkbox and a radio button automatically? I'm just not to sure what to put and where to put it. Your example for the input box really helped me!
nml375 09 Dec, 2009
Hi comraddan,
Radio- and checkboxes require slightly modified code, as each input has a fixed value, and what we'd really like to change is it's state.

<input type="radio" name="radio_1[]" value="some value"<?
if (in_array('some value', explode(', ', $userData->radio_1)))
  echo ' checked="checked"';
?> />

This assumes you've set the "Handle Posted Arrays" option to "Yes" in your form setup.

/Fredrik
comraddan 10 Dec, 2009
Thanks again! Everything seems to be working, and I figured that a dropdown box of states would be helpful for others and save someone some typing (of course id="select_15" and name="select_state" and all the other "select_state" may need to be changed to match your form) or to use as an example:

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">State:</label>
    <select class="cf_inputbox" id="select_15" size="1" title="" name="select_state">
    <option value="">Choose a State...</option>
      <option value="Outside US / Canada">Outside US / Canada</option>
<option value="Alabama"<? if (in_array('Alabama', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Alabama</option>
<option value="Alaska"<? if (in_array('Alaska', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Alaska</option>
<option value="Alberta"<? if (in_array('Alberta', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Alberta</option>
<option value="American Samoa"<? if (in_array('American Samoa', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>American Samoa</option>
<option value="Arizona"<? if (in_array('Arizona', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Arizona</option>
<option value="Arkansas"<? if (in_array('Arkansas', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Arkansas</option>
<option value="Armed Forces Americas"<? if (in_array('Armed Forces Americas', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Armed Forces Americas</option>
<option value="Armed Forces Europe"<? if (in_array('Armed Forces Europe', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Armed Forces Europe</option>
<option value="Armed Forces Pacific"<? if (in_array('Armed Forces Pacific', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Armed Forces Pacific</option>
<option value="British Columbia"<? if (in_array('British Columbia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>British Columbia</option>
<option value="California"<? if (in_array('California', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>California</option>
<option value="Colorado"<? if (in_array('Colorado', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Colorado</option>
<option value="Connecticut"<? if (in_array('Connecticut', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Connecticut</option>
<option value="Delaware"<? if (in_array('Delaware', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Delaware</option>
<option value="District Of Columbia"<? if (in_array('District Of Columbia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>District Of Columbia</option>
<option value="Florida"<? if (in_array('Florida', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Florida</option>
<option value="Georgia"<? if (in_array('Georgia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Georgia</option>
<option value="Guam"<? if (in_array('Guam', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Guam</option>
<option value="Hawaii"<? if (in_array('Hawaii', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Hawaii</option>
<option value="Idaho"<? if (in_array('Idaho', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Idaho</option>
<option value="Illinois"<? if (in_array('Illinois', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Illinois</option>
<option value="Indiana"<? if (in_array('Indiana', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Indiana</option>
<option value="Iowa"<? if (in_array('Iowa', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Iowa</option>
<option value="Kansas"<? if (in_array('Kansas', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Kansas</option>
<option value="Kentucky"<? if (in_array('Kentucky', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Kentucky</option>
<option value="Louisiana"<? if (in_array('Louisiana', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Louisiana</option>
<option value="Maine"<? if (in_array('Maine', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Maine</option>
<option value="Manitoba"<? if (in_array('Manitoba', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Manitoba</option>
<option value="Maryland"<? if (in_array('Maryland', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Maryland</option>
<option value="Massachusetts"<? if (in_array('Massachusetts', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Massachusetts</option>
<option value="Michigan"<? if (in_array('Michigan', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Michigan</option>
<option value="Minnesota"<? if (in_array('Minnesota', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Minnesota</option>
<option value="Mississippi"<? if (in_array('Mississippi', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Mississippi</option>
<option value="Missouri"<? if (in_array('Missouri', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Missouri</option>
<option value="Montana"<? if (in_array('Montana', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Montana</option>
<option value="Nebraska"<? if (in_array('Nebraska', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Nebraska</option>
<option value="Nevada"<? if (in_array('Nevada', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Nevada</option>
<option value="New Brunswick"<? if (in_array('New Brunswick', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>New Brunswick</option>
<option value="New Hampshire"<? if (in_array('New Hampshire', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>New Hampshire</option>
<option value="New Jersey"<? if (in_array('New Jersey', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>New Jersey</option>
<option value="New Mexico"<? if (in_array('New Mexico', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>New Mexico</option>
<option value="New York"<? if (in_array('New York', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>New York</option>
<option value="Newfoundland"<? if (in_array('Newfoundland', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Newfoundland</option>
<option value="North Carolina"<? if (in_array('North Carolina', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>North Carolina</option>
<option value="North Dakota"<? if (in_array('North Dakota', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>North Dakota</option>
<option value="Northern Mariana Is"<? if (in_array('Northern Mariana Is', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Northern Mariana Is</option>
<option value="Northwest Territories"<? if (in_array('Northwest Territories', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Northwest Territories</option>
<option value="Nova Scotia"<? if (in_array('Nova Scotia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Nova Scotia</option>
<option value="Ohio"<? if (in_array('Ohio', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Ohio</option>
<option value="Oklahoma"<? if (in_array('Oklahoma', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Oklahoma</option>
<option value="Ontario"<? if (in_array('Ontario', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Ontario</option>
<option value="Oregon"<? if (in_array('Oregon', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Oregon</option>
<option value="Palau"<? if (in_array('Palau', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Palau</option>
<option value="Pennsylvania"<? if (in_array('Pennsylvania', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Pennsylvania</option>
<option value="Prince Edward Island"<? if (in_array('Prince Edward Island', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Prince Edward Island</option>
<option value="Province du Quebec"<? if (in_array('Province du Quebec', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Province du Quebec</option>
<option value="Puerto Rico"<? if (in_array('Puerto Rico', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Puerto Rico</option>
<option value="Rhode Island"<? if (in_array('Rhode Island', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Rhode Island</option>
<option value="Saskatchewan"<? if (in_array('Saskatchewan', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Saskatchewan</option>
<option value="South Carolina"<? if (in_array('South Carolina', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>South Carolina</option>
<option value="South Dakota"<? if (in_array('South Dakota', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>South Dakota</option>
<option value="Tennessee"<? if (in_array('Tennessee', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Tennessee</option>
<option value="Texas"<? if (in_array('Texas', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Texas</option>
<option value="Utah"<? if (in_array('Utah', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Utah</option>
<option value="Vermont"<? if (in_array('Vermont', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Vermont</option>
<option value="Virgin Islands"<? if (in_array('Virgin Islands', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Virgin Islands</option>
<option value="Virginia"<? if (in_array('Virginia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Virginia</option>
<option value="Washington"<? if (in_array('Washington', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Washington</option>
<option value="West Virginia"<? if (in_array('West Virginia', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>West Virginia</option>
<option value="Wisconsin"<? if (in_array('Wisconsin', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Wisconsin</option>
<option value="Wyoming"<? if (in_array('Wyoming', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Wyoming</option>
<option value="Yukon Territory"<? if (in_array('Yukon Territory', explode(', ', $userData->select_state))) echo ' selected="yes"'; ?>>Yukon Territory</option>

    </select>
    <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
				<div class="tooltipdiv">State: :: Please choose the state where you are located.</div>
  </div>
  <div class="cfclear"> </div>
</div>
GreyHead 10 Dec, 2009
Hi comraddan,

This will probably work but it's not very 'elegant' code and does a lot of uneccessary work - in particular you execute explode(', ', $userData->select_state) some fifty odd times.

One potential problem is that selected="yes" is technically incorrect -- it should be selected="selected" -- so may not work with all browsers.

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