We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25902
    • 27 Posts
    Heyas, I’m using firstname and lastname as custom fields for my webloginpe (register and profile). Is there any way to combine them and write them to the default fullname field?

    Currently, I’m using a method found in the forums which changed a line of code under the Register function:
    from
    $fullname = $modx->db->escape($modx->stripTags($_POST[’fullname’]));
    to
    $fullname = $modx->db->escape($modx->stripTags($_POST[’firstname’].’ ’.$_POST[’lastname’]));

    This works for registration but not for profile updating. Changing the firstname and lastname under Profile and then clicking save does not update the fullname.

    Is there a way to do this through a snippet and placeholders where any calls made for register, profile, etc would change fullname to include firstname and lastname, without changing any code - future-proofing.
    Like so:
    [+fullname+] = [+firstname+] + [+lastname+]??

    Thanks.
      • 7155
      • 160 Posts
      wouldnt using "&customFields" be a better option?

      if I understand correctly you want the person to fill in separately the first name and last name on registering....

      then on profile updating you want one field for first name and surname?

      If thats correct, then why not just combine them on registration and do the same for profile editing OR

      Separate the fields for both registration and profile editing

      Assuming you dont want either option, then you could only assume that every fullname has two words, separated by a space.

      Then on profile editing you would do the following:

      Lets say your text box is fullname

      So on registering you will "explode" the value of fullname into first name and surname



      $fullname = explode(" ",$_POST[’fullname’]);
      $firstname = $fullname[0];
      $surname = $fullname[1];
      //do what you want to do with them

        • 25902
        • 27 Posts
        Aye, I’m using &customfields for both firstname and lastname. What I want to happen is have both values added into fullname upon registration and profile update. Fullname is hidden from the user and automatically updated when the user registers or updates his first and last names.

        What I’m not sure about is how to code it, would this work?

        For registering I call in a snippet containing this:

        <?php
        if( !function_exists(’Register’) ){
        function Register(&$fields){
        $fullname = $modx->db->escape($modx->stripTags($_POST[’firstname’].’ ’.$_POST[’lastname’]));
        }
        }
        ?>

        and then the same for Profile except I change the function to: function SaveUserProfile?

        Sorry, I’m new to ModX and php coding.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Actually that should be a plugin; plugins are code that is inserted into the core processes (whether editing/creating documents, registering or logging in and out of users, or parsing a document to generate a web page) to customize the core behavior. See the R

          In this case, I believe that you would want to have your plugin code use the OnBeforeWebSaveUser event to concatenate the first name and last name into the fullname. Then the usual save of the user data would continue, now with your modified fullname field.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 27442
            • 103 Posts
            But a plugin ala ’OnBeforeWebSaveUser’ only works in the manager, right?

            The only way that I can think to allow webusers to do this from the front end, would be to make a snippet using the dbapi that would concatenate the [newly modified] custom fields and post the update directly to the web_user_attributes table. Personally, for the site that I referenced in http://modxcms.com/forums/index.php/topic,37797.0.html, I just tell the users to send me a note and I do it manually from the manager.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              The WebLoginPE snippet also enables the web user events. See the WebLoginPE documentation in the assets/snippets/webloginpe/docs folder; open the index.html in your browser and see the API tab. These events are selected from the Web Access Service Events group of system events, and are distinct from the events triggered by the Manager’s web user processing events.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                There are two way you could accomplish your purpose.

                One is to use a hidden field in the form and use javascript to populate it from the firstname and lastname fields on submission (or use onblur for each field).

                The second is to use a plugin with the OnWebSaveUser event, get the internalKey (the user ID), firstname and lastname from the $user[] array supplied to that event, concatenate them and insert them into the database.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 25902
                  • 27 Posts
                  Heyas Susan, thanks for the suggestions. However, I’m a newbie and my attempts at making custom scripts were not successful. :’(
                  Please help.

                  This is what I tried first, using the OnBeforeWebSaveUser function.

                  The webloginPE profile call:
                  [!NameCombine!]
                  [!WebLoginPE? &OnBeforeWebSaveUser=’NameCombine’ &type=`profile` &profileTpl=`Profile` &customFields=`firstname,lastname,address1,address2` !]

                  and the snippet:
                  <?php
                  if( !function_exists(’OnBeforeWebSaveUser’) ){
                  function OnBeforeWebSaveUser(&$fields){
                  $fullname = $modx->db->escape($modx->stripTags($_POST[’firstname’].’ ’.$_POST[’lastname’]));
                  }
                  }
                  ?>

                  This did not work sad

                  Next I tried the javascript thingy, here’s my call:

                  [!WebLoginPE? &customJs=`assets/snippets/webloginpe/js/namo.js` &type=`profile` &profileTpl=`Profile` &customFields=`firstname,lastname,address1,address2` !]

                  and the script:
                  function namo(){
                  var f,n,m;
                  n=document.getElementById("wlpeUserProfileFirstName").value;
                  m=document.getElementById("wlpeUserProfileLastName").value;
                  f=n+m;

                  document.getElementById("wlpeUserProfileFullName").value = f;
                  }

                  This did not work too sad

                  Where did I go wrong?

                  Also, can you please show how to use a plugin with the OnWebSaveUser event?

                  Thanks.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    $e =& $modx->event;
                    switch ($e->name ) {
                        case 'OnWebSaveUser':
                                $table = $modx->getFullTableName('web_user_attributes');
                                $fields = array('fullname' => $user['firstname'] . $user['lastname']);
                                $modx->db->update($fields, $table, 'internalKey = ' . $user['internalKey']);
                    }
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 25902
                      • 27 Posts
                      Thank you, Susan smiley
                      I pasted your code into a snippet called NameCombine. And then I added it into my Profile call like so:

                      [!NameCombine!]
                      [!WebLoginPE? &OnWebSaveUser=’NameCombine’ &type=`profile` &profileTpl=`Profile` &customFields=`firstname,lastname,address1,address2` !]

                      I think I’m calling it wrong, it’s not working huh