Page 1 of 1

Pre-Setting some initial form values

PostPosted: Wed Aug 14, 2019 1:49 am
by hdpeterson
Hi.
I'm new to nuBuilder, and have rusty PHP/JS experience. However, I'm trying to build an app to track volunteers for a charity. When a user inputs a new volunteer record, they capture the volunteer's address, along with information like their name and other contact info. I'd like the 'city' field to automatically insert the main city name as a default value, but still allow the user the option to input something different in the field before they save the record. I.e. Most of the volunteers will come from the city itself, but some come from the surrounding county, with smaller communities with their own name.

E.g. The city is Toronto, but some of the volunteers come from Oshawa or Markham, which are smaller communities (and incorporated cities in their own right) outside Toronto proper. So, a new form would automatically put "Toronto" in the city field, but the user could type "Oshawa" into the field to change it before saving the form for the first time.

I've done searches on the forum here for "prepopulating form fields", "set default value", and similar such phrases. I've also read through all the responses I could find. But, I'm not convinced those posts answer my question... or maybe I'm just having difficulty parsing the answers well enough to implement them myself.

Can anyone suggest an easy way to do this, or do I have to do some fancy code work behind the scenes? (I'm not sure where to start with that.) Any suggestions would be greatly appreciated.

Thanks.

Re: Pre-Setting some initial form values

PostPosted: Wed Aug 14, 2019 2:34 am
by Janusz
Hi,
The simplest way is to place the code as example belowe in the:
Form Properties / Custome code /Javascript.
There are two possibilities of the function doing exactly the same thing. And this function is executing only once during the new record creation.
You can place one of them in any part of the form javascript
Code: Select all
if(nuIsNewRecord()){
var city='Toronto';
$('#kon_city').val(city).change();
$('#kon_city2').val('New York').change();
}

OR use this one - they are equivallent.

if(nuCurrentProperties().record_id == '-1')
{
var city='Toronto';
$('#kon_city').val(city).change();
$('#kon_city2').val('New York').change();
}

Re: Pre-Setting some initial form values

PostPosted: Wed Aug 14, 2019 11:15 pm
by hdpeterson
Thanks for the code snippets.

However, I'm not sure it's working. When I try to enter test data into the live form (not the preview), the form comes up entirely blank; the 'city' field is not filled in at all, which suggests to me it's not actually pre-populating as intended.

I think I've put the code in the right spot:

Image

Do I need to adjust something with regard to the field itself in the field properties?

At the end of the day, it's not the end of the world. I was just hoping to spare the users a few key strokes.

Re: Pre-Setting some initial form values

PostPosted: Thu Aug 15, 2019 12:23 am
by kev1n
Does your city field really have the ID kon_city ? You might have to change that.

Re: Pre-Setting some initial form values

PostPosted: Thu Aug 15, 2019 11:13 pm
by hdpeterson
Oops. Yeah. Proofreading helps.

Told you I was a little rusty. :D

Thanks! It works now.

Re: Pre-Setting some initial form values

PostPosted: Fri Aug 16, 2019 8:29 am
by admin
.