javascript help/advice - dealing with tables and eval()

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

javascript help/advice - dealing with tables and eval()

#1

Postby alexmcclune » Mon Nov 18, 2013 12:25 pm

Hi,

I have a form with the usual fields (name, address, details, etc) and this content is saved into a table as name/value pairs. I would then like to repopulate the form at a later date using the name/value pairs to match the content up to the correct form fields.

I have the following code, array just seemed like a sensible choice to easily manage the fields (and I want to re-use the code on other forms).

Code: Select all

var meta_rows = tables.TBL_META.fetchTable();
var fieldsarray = new Array();
fieldsarray[0] = "FRM_TITLE";
fieldsarray[1] = "FRM_FIRSTNAME";
fieldsarray[2] = "FRM_LASTNAME";
fieldsarray[3] = "FRM_ADDRESS1";
fieldsarray[4] = "FRM_ADDRESS2";
fieldsarray[5] = "FRM_ADDRESS3";
fieldsarray[6] = "FRM_ADDRESS4";
fieldsarray[7] = "FRM_POSTCODE";
fieldsarray[8] = "FRM_TELEPHONE";
fieldsarray[9] = "FRM_EMAIL";
fieldsarray[10] = "FRM_DETAILS";
fieldsarray[11] = "FRM_DETAILS_MORE";
fieldsarray[12] = "FRM_OTHER_DETAILS";

while (meta_rows.next()){
	if ( system.executeCustomFunction("contains", [tables.TBL_META.FLD_ID.value, "FRM_"])){
		if ( tables.TBL_META.FLD_STRING_ID.value == 0 ){
			for &#40;var i=0; i < fieldsarray.length; i++&#41;&#123;
					var fieldname = "fields."+fieldsarray&#91;i&#93;+".value";	
				if &#40; tables.TBL_META.FLD_ID.value == fieldsarray&#91;i&#93; &#41;&#123;
					var x = fieldname + "='" + tables.TBL_TESTCRM_PPCALLMETADATA.FLD_VAL.value + "';";
					eval&#40;'x'&#41;;
				&#125;
			&#125;
		&#125; // somethig else here
	&#125;
&#125;

EDIT - I have managed to get around all my errors but unfortunately the form does not populate. When I log the value of "x" it is in the correct format but the eval() does not execute it - the form fields are not set.

Please forgive any coding bad practice, I am pretty damn new to Javascript so learning as I go.
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#2

Postby dvanhussel » Mon Nov 18, 2013 1:43 pm

Hi,

I think that because you have x within '', it is evaluated as a string, not as the value of the var.

I think this will work:

Code: Select all

var x = '\''+fieldname+'='+tables.TBL_TESTCRM_PPCALLMETADATA.FLD_VAL.value+'\'';
eval&#40;x&#41;;
But, why do want to do it like this?

If you map the form fields to the corresponding fields of the database resource, you only have to fetch it to populate the form fields with the correct data.
Last edited by dvanhussel on Mon Nov 18, 2013 1:50 pm, edited 1 time in total.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#3

Postby Jon » Mon Nov 18, 2013 1:49 pm

I think it should be

Code: Select all

eval&#40;x&#41;;
instead of

Code: Select all

eval&#40;'x'&#41;;
P.S. instead of using the FPL contains function, you can do:

Code: Select all

if &#40;tables.TBL_META.FLD_ID.value && tables.TBL_META.FLD_ID.value.indexOf&#40;"FRM_"&#41; != -1&#41;
..
The first part of this checks for a null value and the second part does the contains check. It will be much faster.

Regards
Jon
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#4

Postby alexmcclune » Thu Nov 21, 2013 12:37 pm

Thanks, I have got it working now - using too many "'" and you were right jon, the 'x' was incorrect. Many thanks.

The database table I am grabbing data from essentially has 3 fields (although in reality it has more and I am grabbing data from 2 tables with similar structure - only 1 will be populated).

Anyway the table has 3 fields, a call_id, the field_ID and the Field Value:
- call id is a unique reference
- field_id is the form_field name
- field_val is the form_field value

Can you think of a way to map that resource to a form with numerous fields (of which the only way to determine the content is by comparing the form field name to the value of field_id in the table)?

I briefly tried and couldn't see an obvious method, I created the javascript as the solution but if there is an easier way I would love to hear it?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Thu Nov 21, 2013 1:53 pm

I don't think there's an easier way. If I was doing this I would code the SQL statement in the Javascript then find each field dynamically using fields.getField(xxx). Probably the way you've done it by the sound of it.
0 x


Who is online

Users browsing this forum: No registered users and 9 guests