Pre-Setting some initial form values

Questions related to customising nuBuilder Forte with Javascript or PHP.

Pre-Setting some initial form values

Postby hdpeterson » Wed Aug 14, 2019 1:49 am

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.
hdpeterson
 
Posts: 3
Joined: Wed Aug 14, 2019 12:34 am

Re: Pre-Setting some initial form values

Postby Janusz » Wed Aug 14, 2019 2:34 am

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();
}
Janusz
 
Posts: 255
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Pre-Setting some initial form values

Postby hdpeterson » Wed Aug 14, 2019 11:15 pm

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.
hdpeterson
 
Posts: 3
Joined: Wed Aug 14, 2019 12:34 am

Re: Pre-Setting some initial form values

Postby kev1n » Thu Aug 15, 2019 12:23 am

Does your city field really have the ID kon_city ? You might have to change that.
kev1n
 
Posts: 358
Joined: Mon Oct 15, 2018 2:13 am

Re: Pre-Setting some initial form values

Postby hdpeterson » Thu Aug 15, 2019 11:13 pm

Oops. Yeah. Proofreading helps.

Told you I was a little rusty. :D

Thanks! It works now.
hdpeterson
 
Posts: 3
Joined: Wed Aug 14, 2019 12:34 am

Re: Pre-Setting some initial form values

Postby admin » Fri Aug 16, 2019 8:29 am

.
admin
Site Admin
 
Posts: 3162
Joined: Mon Jun 15, 2009 9:53 am


Return to Custom Code



cron