How to add the logged in user to a contact form 
Autor: Vagn M.
Visited 468,
Followers 1,
Udostępniony 0
I am trying to figure out how I can add a username a logged in user to a contact form.
Is it possible or do I need to develop a contact form myself?
Best Regards
Vagn Majland
Posted on the
Hello Vagn,
Into your page add an HTML object and copy paste this code.
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo "Bienvenue : ".$user['realname'];
?>
<br> </br>
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo "Bienvenue : ".$user['username'];
?>
<br> </br>
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo "Bienvenue : ".$user['uid'];
?>
Here 3 different displaying
And VERY IMPORTANT. The page needs to be private for the user / group
And the result is:
Hope this helps
Axel
Autor
Hi Axel
Thank you for the prompt answer.
Unfortunately the answer does not solve our problem. We already have this code in the solution:
<?php
require_once"res/x5engine.php";
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo "<script>";
echo "localStorage.setItem('Fornavn', '".$user['firstname'] ."');";
echo "localStorage.setItem('Efternavn', '".$user['lastname'] ."');";
echo "localStorage.setItem('Email', '".$user['email'] ."');";
echo "localStorage.setItem('Brugernavn', '".$user['username'] ."');";
echo "localStorage.setItem('UID', '".$user['uid'] ."');";
?>
It is working very well and we can all over the site get, manage, show and print the information.
But how can we get teh information inserted into a field in the contact form? E.g. in the Name field where you see the arrow:
Best Regards
Vagn
By CSS I suppose.
Hey KolAsim, can you help us ?
Axel
... (It > En)
1) ... post the LINK to the relevant page with the contact form;
2) ... apply the EXTRA PHP code to detect the logged-in user visible on the page;
3) ... create a fully enabled dummy user "pippo" with:
- user: pippo(At).xx.yy ... (AT) = @
- pas: 12345
...
... if this is done and I see this, I'll come back here...
.
ciao
.
Hey KolAsim
thank you for your help
demo is ready here
page: https://wsx5demo.afsoftware.fr/bb.php
login: demo at demo.com
pass: password
Axel
...incorrect password...
.
it works fine from my side... check again
Autor
I succeede on my first login, but now I get this message:
Best Regards
Vagn
go to myaccount page and force a logout
check again
Autor
Now I got access again.
I entered some information and send it.
But I can't see the result.
And I am also just a little confused about what to do to build the same functionality up here in Denmark.
Best Regards
Vagn
Hey Vagn
my page does not work to populate the field
so we are waiting KooAsim´help to do it
axel
.
Use ENTER on the keybord instead of clicking the button
I dont understand
works on my phone too
here after authentication !!!!
@John
it works from your side ?
Axel
@Axel
No.
It does not work in Chrome or Firefox when I use the click-button.
When I use the button I get the same message as KolAsim.
But when I press ENTER it functions.
... OK... after a few tries it worked, but it's not regular...

... it would be better if the email text was in a single object with just the email and not like it is now with other text...
... however, for the current situation you can use this code of mine with which I extracted the email, obtaining what you see in my STAMP:
<script>
/** -- assegna email automaticamente da utente loggato x WSx5 by KolAsim --- **/
$(document).ready(function () {//K>;
setTimeout(function () {//K1>;
str_emailK = $("#imHTMLObject_343_02").text();
var estraeEmailK = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
var emailEstrattaK = str_emailK.match(estraeEmailK);
if (emailEstrattaK) {//K2>;
$(".valEmail").val( emailEstrattaK[0]);
$(".valEmail").prop('disabled', true);
} ;//K2<<;
}, 3000);//K1<<; // ritardo 3000 (ms) = 3 (sec) da valutare --:
});//K<<;
</script>
.
ciao
.
@John,
OK, I have done a post regarding this issue.
@KolAsim
AGain, great job
You are right, should be easier to have each line into one text object to avoid extraction like you have done.
@Vagn
Now you have the KolAsim script to put automatically email into the field.. You just need to authentificate the field name by an inspect into your browser
Here the email field is #imHTMLObject_343_02
Like into inspect
Axel
...I checked, ...OK...it works...
.
Autor
Thanks to you all for your efforts in this case.
My only problem now is that I am not sure about how to implement script including the php.
If possible - please give me screenshots from the test site.
Best Regards
Vagn
@KolAsim,
With your script, any quick idea to copy the name into the field form contact
Axel
Hey KolAsim,
Maybe easier, I have created 2 object for username and email like your recommandation.
Here the new page:
https://wsx5demo.afsoftware.fr/bb1.php
And I have just added this line into the script to retrieve the username.
/* to retrieve logged username */
str_nameK = $("#imHTMLObject_344_06").text();
But How tu fulfill the form name with the username.
Thank you
Axel
...!... it's better to use a dedicated HTML/PHP object to retrieve:
echo $user['username'];
...however, my code adapted for the current situation is this:

<script>
/** -- estrae ed assegna nome in form_mail automaticamente da utente loggato x WSx5 by KolAsim --- **/
$(document).ready(function () {//K>;
setTimeout(function(){//K1>;
var nomeK = $("#imHTMLObject_343_02").contents().filter(function() {//K2>;
return this.nodeType === 3 && $.trim(this.nodeValue) !== "";
});//K2<<;
nomeK = $(nomeK).first().text().trim();
$("#imObjectForm_3_1").val(nomeK);
},3000); //K1<<; ritardo 3000 (ms) = 3 (sec) da valutare --
});//K<<;
</script>
.
ciao
.
Again thank you KolAsim
@Vagn
be patient, I am'going to prepare explanation for you
Hello Vagn,
here explanations.
Need to create a page with HTML object and form object like below.
And configure this page like private with authentication.
Into HTML object I put my code to get logged user's information.
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo $user['realname'];
?>
<br> </br>
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
echo $user['username'];
?>
<br> </br>
<?php
$pa = Configuration::getPrivateArea();
$user = $pa->whoIsLogged();
// echo $user['uid'];
?>
And my form object is simple
Before any thing you need to get the html name of your field where the name and email are displaying by an Inspect command with Google Chrome and right click on the username
And the inspect window give your the field name of your object
You need also to get the field name of your user name and email address by same way.
and email address
In the second HTML object (bottom of my page) you need to copy this code.
<script>
/** -- estrae ed assegna nome in form_mail automaticamente da utente loggato x WSx5 by KolAsim --- **/
$(document).ready(function () {//K>;
setTimeout(function(){//K1>;
/* To retrieve the logged user name */
var usernameK = $("#imHTMLObject_343_02").contents().filter(function() {//K2>;
return this.nodeType === 3 && $.trim(this.nodeValue) !== "";
});//K2<<;
/* To retrieve the logged user name email address */
str_emailK = $("#imHTMLObject_343_02").text();
/* To extract email address from HTML string */
var estraeEmailK = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
var emailEstrattaK = str_emailK.match(estraeEmailK);
/* To populate the form email field with the logged user name email address */
/* and to disable the field */
$(".valEmail").val( emailEstrattaK[0]);
$(".valEmail").prop('disabled', true);
/* To populate the form name field with the logged user name */
/* and to disable the field */
usernameK = $(usernameK).first().text().trim();
$("#imObjectForm_5_1").val(usernameK);
$("#imObjectForm_5_1").prop('disabled', true);
},500); //K1<<; delay in (ms) = 3000 = (sec) of delay --
});//K<<;
</script>
And here you need to change the bold names by yours founded just previsously.
And you should have something like that
I you don't want to see the logged user's informations on your page you can hide the HTML object where the code is by this way.
And the displaying will be..
Hope this helps.
Axel
@KolAsim,
Any idea.
If the copied fields into the form are disabled the email received is not correct.
Need to put them into comment to receive the email with the values !
@Vagn
For now into my previous code remove these 2 bold lines or put them into comment
/* To populate the form email field with the logged user name email address */
/* and to disable the field */
$(".valEmail").val( emailEstrattaK[0]);
$(".valEmail").prop('disabled', true);
/* To populate the form name field with the logged user name */
/* and to disable the field */
usernameK = $(usernameK).first().text().trim();
$("#imObjectForm_5_1").val(usernameK);
$("#imObjectForm_5_1").prop('disabled', true);
Autor
@Alex
Thank you for the guide lines.
It is almost working. The e-mail is set correctly, but the name is not. I am trying to figure out why.
The name is displayed in the top as you see in the image below. The code is copied exactly from your post and the modifications are done as described.
The most important is the e-mail address, so it will work with that part only, but it will sure be better with both fields.
Ctrl + F5 many times to force browser refresh
double check the code and object names from inspect
post the link with credentials to see
Autor
I am not sure if this should be modified:
$("#imObjectForm_5_1").val(usernameK);
Yes you need by your object name (inspect)
Autor
Now everyting is working perfectly.
There are one thing you have to be aware of:
If you activate this: $(".valEmail").prop('disabled', true); the database is not updated. If you don't activate it the user can modify the field. Both will cause problems.
But if you make the fields hidden: $(".valEmail").prop('hidden', true); everything is just like we need it.
-----------------------------------------------
This wa a hard but succesfull tread.
I am really happy that I have "met" you @KolAsim and @Axel here in the community. I really hope I can reciprocate one day.
I have worked as a programmer/system developer for 54 years, but am no longer employed.Since I haven't left the industry for a few years, I have been introduced to Website X5 and PHP, which has already brought in several customers.
This community has been very helpfull, but no one surpasses your efforts. Hope we "meet" again and that I can give something back.
Thank you...
Best Regards
Vagn