How to capture $memberinfo with Javascript

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

How to capture $memberinfo with Javascript

Post by buck1942 » 2016-09-01 23:25

The following works just fine:

$j('#first_name').val("JACK");

however...

I am having trouble retrieving #first_name from {$memberinfo['username']}; using javascript...

Buck

peebee
AppGini Super Hero
AppGini Super Hero
Posts: 352
Joined: 2013-03-21 04:37

Re: How to capture $memberinfo with Javascript

Post by peebee » 2016-09-02 00:16

You need to get the $memberinfo array first by calling the function getMemberInfo()

$memberInfo = getMemberInfo();

See here: https://bigprof.com/appgini/help/advanc ... Info-array

Just a warning as I'm not entirely sure what you're trying to achieve but javascript won't retrieve the $memberInfo array "on the fly" (while you are looking at an open record).

Depending on what you're after; If you are just wanting to enter a Username into a field on creation or update, you might be better off using one of AppGini's built in "Default" values (<%%creatorUsername%%> or <%%editorUsername%%>) or even a simple hook rather than javascript? Just a thought....

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to capture $memberinfo with Javascript

Post by buck1942 » 2016-09-02 01:13

peebee... my original intent was to have the user's name displayed, on the input screen, while they were inputting data... certainly not necessary and serves very little purpose. I have several fields that are filled in as soon as the form is loaded and I tho't it might be neat to have the user's name on the screen. It is a math application for students in the 8-12 range or so... they kinda like to see there name.

I have been using Appgini for about 10 years, but only for a database manager/file maintenance, and it works very well for that... only recently have I decided to use HOOKS. I am catching on, but a little confused when to use "$data(fields name)" or "$fieldname" or just "fieldname" or "#fieldname" etc.

I'll just keep pluggin' away... you and grimblefritz have been of big help, but I do not want to wear out my welcome.

Thanks,

Buck

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to capture $memberinfo with Javascript

Post by grimblefritz » 2016-09-02 02:41

Buck,

It helps to bear in mind the nature of web apps. There's server component and a client component, and specific roles and limitations for each.

Just keep in mind that PHP hooks run on the web server and have access to the sql database and the various vars/arrays/objects defined in PHP. That all runs as the page is being constructed, or after the page is submitted (saved.) In that regard, the PHP elements are one-shot, in that they run to construct the page or to parse the page, but don't do anything interactively while the page is being used.

Javascript runs in the browser and can only deal with the page after it's been constructed by the server and sent to the browser to be rendered. It can also interact with the page while it's being used. It cannot read or write the sql database. (Unless using ajax, but then that's not exactly javascript, but basically like a separate page request/generate/send/parse event.)

Keeping in mind WHEN you need to create an action, will help you decide WHERE to code it. And that will determine HOW things are named and accessed, etc.

And, when in doubt, ask. You might not the only one wondering, so an answer to you could be an answer to several.

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to capture $memberinfo with Javascript

Post by grimblefritz » 2016-09-02 02:49

Also, if it makes you feel any better, I've also had AppGini for years and used it only for basic database management. But with the recent releases I've found it acceptable for basic applications, and with fewer demands on my time over the last year or two I've had more time to delve into hooks.

Because I don't like to be orphaned when new versions make major changes in core code, I try to do everything custom via hooks. That way, if AppGini 5.x or 6.x makes serious changes in the core, I don't have a bunch of hacks to port forward. At worst, just a few hooks to massage. I'd rather code a bit more in hooks, perhaps less elegantly than if I modified the core, than expose myself to being locked out of upgrades due to the cost of porting core code modifications.

My $0.02 fwiw

grimblefritz
AppGini Super Hero
AppGini Super Hero
Posts: 336
Joined: 2015-12-23 16:52

Re: How to capture $memberinfo with Javascript

Post by grimblefritz » 2016-09-02 02:55

Oh, and if you absolutely must have $memberinfo available in javascript, here's one way to do it.

Or, rather than expose all the details in memberinfo, since what you want is the username...

1. In tablename.php function tablename-dv(), inject a hidden field with id=membername and value=name (name coming from $memberinfo)

2. In tablename-dv.js use jquery to get the membername, as in: membername=$j('#membername').val();

You could, if you wanted, then use jquery to delete #membername so it's not seen if someone views page source. Not foolproof, but fools most.

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to capture $memberinfo with Javascript

Post by buck1942 » 2016-09-02 04:20

grimble,

Good synopsis... and thank you for taking the time

Buck

peebee
AppGini Super Hero
AppGini Super Hero
Posts: 352
Joined: 2013-03-21 04:37

Re: How to capture $memberinfo with Javascript

Post by peebee » 2016-09-02 04:30

Fortunately for you Buck, I had reason to grab the $memberInfo to inject into some javascript in a previous project I was working on. I used AJAX and JSON.

Everything you need to know to achieve what you're after should be contained in this post: http://forums.appgini.com/phpbb/viewtop ... Javascript

Good luck

buck1942
Veteran Member
Posts: 31
Joined: 2016-08-04 02:38

Re: How to capture $memberinfo with Javascript

Post by buck1942 » 2016-09-03 05:04

peebee... thank you, I will take a look.

Buck

sacgtdev
Veteran Member
Posts: 75
Joined: 2020-06-10 11:14

Re: How to capture $memberinfo with Javascript

Post by sacgtdev » 2021-07-09 09:39

Anyone could offer help here? I need to parse the php variable to the javascript in js file as follows:

//table-tv.js for mass update. Want to replace the selection drop down list with a php return value

Code: Select all

// part of the code//
	/* prepare select2 drop-down inside modal */
	$j('#mass-update-new-value-modal').on('shown.bs.modal', function () {
		$j("#mass-update-new-value").select2({
			width: '100%',
			formatNoMatches: function(term){ return massUpdateTranslation.noMatches; },
			minimumResultsForSearch: 5,
			loadMorePadding: 200,
			data: [{"id":"user3","text":"user3"}], //Replacing value with js variable eg. repvalue
			escapeMarkup: function(str){ return str; }
		}).select2('focus');
	});
//js code (Don't know which file to place the js code as it contains php script)

Code: Select all

var repvalue = "<?php echo json_encode($repValue); ?>"
// php code - prepare the list for drop down value (Dunno place at which php file)

Code: Select all

$replist = array("user1","user2");

$repValue = [];
foreach ($replist as $a){
  $repValue[] = array("id" => $a,"text" => $a);

}

$fixed = array("id" => "user3","text" => "user3");

$repValue[] = $fixed;  //will output [{"id":"user1","text":"user1"},{"id":"user2","text":"user2"},{"id":"user3","text":"user3"}]

Post Reply