java script to fill form element from two other elements on the same form

I have my juser form which has the user first and last name as elements.

I also have another element for the full name and it would look like this.

Doe, John

Then there is the username field which is the user first and last name together

johndoe.

I want to use java script to populate these fields so I don't have to type the information over and over.

Unfortunately I do not know js and cannot find any direction by searching web or the forums.

Thx

Josh
 
The good news is that you don't need JS to do that.
You can use a "calc" element (to find in the GitHub files) to acheive what you want.

Be careful though with the username : if you have more than one "John Doe" registering, this won't work (each username has to be unique).
 
I looked at the calc function, and I am not understanding the process. I was tinkering around with js about a week ago and did this with out any problems.... I just dont remember how I did and cant find the wiki that I used. I believe I could also use the js to query the jos_username table and determine if the user is there and if so append the username with a incrementing value
 
In the calc function under options you can add any php code you wish, including accessing the database. Once you have done all the manipulation you want simply return the text string you want to be in that field.
 
I tried the php script as given. This is what I put in.

return "{ registered_students___lastname , registered_students___firstname}";

nothing populated in the field this was set on.....
 
Nope, you haven't tried "the php script as given". Look at what you quoted, then at what Rob suggested. Spot The Difference.

-- hugh
 
return "{registered_students___lastname}, {registered_students___firstname}";

Ok fixed that.... For the full name element this works great...... you only see the change on save... even though only calc on save is set to no....

But for the students username this will not work due to the field not being editable and in the case of the same username already being in use. I believe this is why I was looking for the js to populate the field after the first and last name has been typed in. I tried to read the js wiki but got confused. Ill try to go back and read again...
 
for the readonly - the calc should still work, regarless of whether the calculation element is set to read only or not, the value will still be shown.

For the username check, it depends what you want to do, if you find an existing user, but say for example you could do this:

PHP:
$username = "{registered_students___lastname}.{registered_students___firstname}"; 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('username')->from('#__users')->where('username LIKE ' . $db->quote($username . '%')->order('username DESC');
$users = $db->loadColumn();
if (empty($users))
{
  return $username;
}
$lastUser = $users[0];
$lastUserNum = (int) str_replace($username, '', $username);
$newUserNum = $lastUserNum + 1;
$newUserName = $username . newUserNum;
return $newUserName;

this would create a username "john.doe"
then if another john.doe signs up his username would be "john.doe1", then "john.doe2" etc.

Note I've not tested the code above but the principle I think is correct
 
this may work but it will not pass the joomla username requirements due to the "." between the first and last name....

i tried taking out the . and still go the error ABC_09
 
I tried this once more and attached is a photo of the error recieved.

I turned on the ajax calculation, as i tab forward in the field I see the little loading circle pop up but nothing populates in the calculation
 

Attachments

  • Picture1.png
    Picture1.png
    295.5 KB · Views: 157
Just tried to use this code again on another site and I am not getting any value returned on the calc????

PHP:
$username = "{registeredusers___firstname}{registeredusers___lastname}"; 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('username')->from('#__users')->where('username LIKE ' . $db->quote($username . '%')->order('username DESC');
$users = $db->loadColumn();
if (empty($users))
{
  return $username;
}
$lastUser = $users[0];
$lastUserNum = (int) str_replace($username, '', $username);
$newUserNum = $lastUserNum + 1;
$newUserName = $username . newUserNum;
return $newUserName;
 
I see a syntax error in there, missing close paren on the where() ...

Try this:

PHP:
$username = "{registeredusers___firstname}{registeredusers___lastname}"; 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('username')->from('#__users')->where('username LIKE ' . $db->quote($username . '%'))->order('username DESC');
$users = $db->loadColumn();
if (empty($users))
{
  return $username;
}
$lastUser = $users[0];
$lastUserNum = (int) str_replace($username, '', $username);
$newUserNum = $lastUserNum + 1;
$newUserName = $username . newUserNum;
return $newUserName;

-- hugh
 
Ok I found the syntax error here it is missing a ($) on a the vairiable newUserNum

So the code should be:

PHP:
$username = "{registeredusers___firstname}{registeredusers___lastname}"; 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('username')->from('#__users')->where('username LIKE ' . $db->quote($username . '%'))->order('username DESC');
$users = $db->loadColumn();
if (empty($users))
{
return $username;
}
$lastUser = $users[0];
$lastUserNum = (int) str_replace($username, '', $username);
$newUserNum = $lastUserNum + 1;
$newUserName = $username . $newUserNum;
return $newUserName;

But still have a small issue... Any user name you input always returns with the number 1 after it even users that do not exsist in the db....
 
Also, with the code as is, it'll mangle usernames which are a subset of a longer name. So if you have a johnsmith, and submit a username john, it'll still think john is a duplicate.

So try this:

PHP:
$username = "{registeredusers___firstname}{registeredusers___lastname}"; 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('username')->from('#__users')->where('username LIKE ' . $db->quote($username . '%'))->order('username DESC');
$users = $db->loadColumn();
if (empty($users))
{
   return $username;
}
$newUserName = $username;
$lastUser = $users[0];
$lastUserNum = str_replace($username, '', $username);
if (empty($lastUserNum) || preg_match("#^\d+$#", $lastUserNum)) {
   $lastUserNum = (int) $lastUserNum;
   $newUserNum = $lastUserNum + 1;
   $newUserName = $username . $newUserNum;
}
return $newUserName;

-- hugh
 
Just tested this with a name not in the db and still appends with the number 1 I am going to do some further testing with the code to see if I can come up with a solution.... Has anyone else tested this?
 
Just to confirm ... is this code running in a calc element?

And are you getting that result through an AJAX calc on the form page?

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top