![]() ![]() I'm interested to learn how to develop blocks, themes, plugins etc. Perhaps the rest of this post would be better handled in an e-mail conversation. Maybe I should have posted that code as a zipped block instead, but I wasn't sure how much Kimber knows about Moodle. $PAGE->requires->js('/blocks/block_name/javacode.js') In fact, I took Kimber's GetUser function idea to setup a test block and called the javascript using the appropriate function: I just started reading the "JavaScript guidelines" yesterday (I hope I'm not coming across as condescending because that's not what I intended at all) ![]() However, my thoughts at the time were: is this a good idea to show it this way?īut I just figured: why not just show it this way as a straight-forward example with all details included? After all, a php file containing that code, for all intents and purposes, will run when loaded from a browser. In fact, I consider it a kind of honour based on what I've read on your blog and these forums. It's always good to look at the basic functionality before diving into these things.Įven setting something like this up in a Moodle2 block is slightly challenging but it is a good way to start learning how blocks work. Now working through this example I have figured out how I might test my own AJAX. (as it might appear in a php file using mostly html code - with the php code and username variable highlighted in bold) If you actually want to pass the value of $USER->username to a Javascript function you can normally do so by generating the relevant code in PHP. I just thought I would explain a little further to build on this a bit. I guess jquery support is pretty limited - or so I have read.Īs Chris said, it's not possible to retrieve Moodle's global variables or retrieve results from PHP functions from Javascript. In fact, I'm having a lot of trouble right now trying to get a basic AJAX search function to return values to a DIV tag in Moodle 2. Is what I'm attempting even possible with this method? If not, how else should I go about getting the value of a php variable ($USER->username)to use in a javascript function? AJAX? But just getting the username to display onscreen via a javascript function would let me know that I'm calling it correctly. My ultimate end goal isn't really to have the username display in a text field within a label, but rather to use the username in a javascript encryption function. This is the Javascript, which I'm calling from the Get User button onclick event:ĭ = externalUserID The literal text username ?> is displayed in the text field, rather than the value of the php variable, regardless of whether I use single or double quotes around the in the Javascript. I'm relatively new to programming, but it seems like this should be pretty simple. The first step is to have a form (called GetUserForm) in a Moodle label with a blank text field (called txtusername) and a button called "Get user." When the Get User button is clicked, I want the Moodle username to display in the text field. I'm trying to grab the Moodle username variable to use in a simple Javascript function. ![]()
0 Comments
Leave a Reply. |