I'm redesigning an existing application form in Vue JS that interacts and posts via PHP. The form contains a large number of fields, most contain a HTML ID of something. I have a range of Vue JS methods, one of which parses the Vue JS form data and then submits this via a HTTP POST request. I'm getting an error with part of the code which seems to be thinking that my element.name
is null
.
I didn't write the code, and am simply debugging and attempting to fix this.
I've tried console logging my data, and I appear to indeed be getting a null
value, I'm trying to figure out whether I may need to declare var
or let
infront of my element
variable which is getting some property back.
The method in question causing the error which is called when submitting the form, is listed below:
/**
* Parse Form Data
* @return {object}
*/
parseFormData: function parseFormData() {
console.log('Data ->' + data)
expanded = ''
data = this.formData
ignore = [
'AddressSelection', 'NextPaydateDay', 'NextPaydateMonth',
'NextPaydateYear', 'AskDebtsQuestion', 'loanreason',
'loanreason-select', 'PersonalDebtQuestion'
]
for (property in data) {
if (ignore.includes(property)) continue // skips
element = document.getElementById(property)
console.log('Element ->' + element)
switch (property) {
case 'AppLoanPurposeOther':
expanded += 'data[ApplicationPayday][AppLoanPurposeOther]=' + this.formData.AppLoanPurposeOther
break;
case 'EmpEmployerName':
expanded += 'data[ApplicationPayday][EmpEmployerName]=' + this.formData.EmpEmployerName
break;
case 'EmpEmployedYears':
expanded += 'data[ApplicationPayday][EmpEmployedYears]=' + this.formData.EmpEmployedYears
break;
case 'EmpEmployedMonths':
expanded += 'data[ApplicationPayday][EmpEmployedMonths]=0'
break;
case 'EmpIndustry':
expanded += 'data[ApplicationPayday][EmpIndustry]=' + this.formData.EmpIndustry
break;
case 'CheckDebtAdviceTeam':
expanded += 'data[ApplicationPayday][CheckDebtAdviceTeam]=' + this.formData.CheckDebtAdviceTeam
break;
case 'AppWorkPhone':
expanded += 'data[ApplicationPayday][AppWorkPhone]=' + this.formData.AppMobilePhone
break;
case 'AppHomePhone':data[ApplicationPayday][CheckDebtAdviceTeam]
expanded += 'data[ApplicationPayday][AppHomePhone]=' + this.formData.AppMobilePhone
break;
case 'CheckDebtAdviceTeam':
expanded += 'data[ApplicationPayday][CheckDebtAdviceTeam]=' + this.formData.CheckDebtAdviceTeam
break;
case 'CheckPhoneContract':
expanded += 'data[ApplicationPayday][CheckPhoneContract]=' + this.formData.CheckPhoneContract
break;
case 'BankSortcode':
expanded += 'data[ApplicationPayday][BankSortcode]=' + this.formData.BankSortcode.replace(/-/g , '')
break;
default:
if (element.name === '') continue
console.log('Element Name ->' + element.name)
var string =
expanded += element.name + '=' + data[property]
}
expanded += '&'
}
expanded = encodeURIComponent(expanded.substring(0, expanded.length - 1))
expanded = expanded.replace(/%3D/g , '=')
expanded = expanded.replace(/%26/g , '&')
expanded = expanded.replace(/%20/g , '+')
return expanded
}
The expected result shouldn't be giving me a null
value, the error appears on:
if (element.name == '') continue
The error in the console shows:
tlp_aff_id
is passed in the URL via: ?aff_id=something
and this does work, it's listed in my data as:
tlp_aff_id: '<?php echo urldecode($params['aff_id']) ?>'