Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Pre-Setting some initial form values

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
hdpeterson
Posts: 3
Joined: Tue Aug 13, 2019 5:04 pm

Pre-Setting some initial form values

Unread post 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.
Janusz
nuBuilder Team
Posts: 508
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 10 times
Been thanked: 18 times

Re: Pre-Setting some initial form values

Unread post 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();
}
If you like nuBuilder, please leave a review on SourceForge
hdpeterson
Posts: 3
Joined: Tue Aug 13, 2019 5:04 pm

Re: Pre-Setting some initial form values

Unread post 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.
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Pre-Setting some initial form values

Unread post by kev1n »

Does your city field really have the ID kon_city ? You might have to change that.
hdpeterson
Posts: 3
Joined: Tue Aug 13, 2019 5:04 pm

Re: Pre-Setting some initial form values

Unread post by hdpeterson »

Oops. Yeah. Proofreading helps.

Told you I was a little rusty. :D

Thanks! It works now.
admin
Site Admin
Posts: 2822
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: Pre-Setting some initial form values

Unread post by admin »

.
Post Reply