Getting JS var from rendered page to JS validate script

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Getting JS var from rendered page to JS validate script

Post by jmcgov » 2019-07-10 17:35

Hi AppGini'ers

I have a form, say "myform", which contains a hidden field quantityMax
I have a validation script, say myform-dv.js (based on the UDEMY lessons)

I see that there is an array in the rendered form containing the value I want, within script tags - var data = {quantityMin: "200", quantityMax: "500"};

How can I pass the data() value to the validation function in myform-dv.js file?

Alternatively,
I have a hook file, say myform.php (which has a value for the $quantityMax field, taken from the DB)
How can I pass the $quantityMaxvar value in php to the JS script?

TIA, James

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Getting JS var from rendered page to JS validate script

Post by pbottcher » 2019-07-13 22:06

Hi,

I'm not sure if you can directly get the variable. But you can retrieve in myform-dv.js the part that contains the relevant data by

e.g.

Code: Select all

$j("script:contains('var data = {quantityMin:')").text()
then you get a string that you need to manipulate to your needs.

Hope that gives a starting point.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: Getting JS var from rendered page to JS validate script

Post by jmcgov » 2019-07-15 23:31

Yes it does pbottcher, thanks.James

Post Reply