Displaying username in page textbox

Displaying username in page textbox

by David Swan -
Number of replies: 3

Hi I am new to using moodle and I am trying to display the value of a logged in user with a textbox. the end goal is to create a suggestion box to allow users to put forward ways of improvement, however I cannot get the text to display the username correctly. Instead the value I enter in my HTML code is displayed instead. 

Screenshot of textbox

Within my php i have declared variables for the username

$_SESSION["firstname"] = $USER->firstname;

$_SESSION["lastname"] = $USER->lastname;

$_SESSION["username"] = $USER->username;

and withing my HTML file i am passing the username value this way.


<input name="firstname" style="height: 27px; width: 260px; margin: 10px 10px; color: #231f20;

                border: 1px solid #dedede; -moz-border-radius: 5px; -webkit-border-radius: 5px;

                border-radius: 5px;" type="text" value="<?php echo htmlentities($username) ?>"  />


Can any offer some advice on what I am doing wrong? 





Average of ratings: -
In reply to David Swan

Re: Displaying username in page textbox

by Cristian Martin Nieto -

Hi David,

Sorry if i got it wrong but I understood you created an HTML file somewhere? If you want to output some HTML you shouldn't create a file for it. You should put it either in a theme template or into a block, I'd reccomend you to have a look at this page: https://docs.moodle.org/dev/Blocks

The simplehtml block example should be enough for what you want to do. You will be able to echo your html code from the get_content() function of your block plugin.

In reply to David Swan

Re: Displaying username in page textbox

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Well, first thing that pops into my mind is that your code says input name="firstname" yet you are echoing the username. Is this intentional?

The second is why not just echo $USER->username in the first place?


In reply to Richard Oelmann

Re: Displaying username in page textbox

by David Swan -

hi thank you for taking the time to answer, I'm very new to php and moodle coming from a C# back ground.

thanks for the advice I managed to resolve it eventually.