I actually had to change this slightly as the data wasn’t being saved to my database collection, the fetch() function needed moving to the backend.
1st is in the page code & then 2nd part of this code is saved in the backend as aModule.jsw.
import { local } from ‘wix-storage’ ;
import { fetchMe } from “backend/aModule.jsw”
$w . onReady ( function () {});
import { fetch } from ‘wix-fetch’ ;
export function submit_click ( event ) {
let crmApiURL = ‘https://www.nationaldebtassistance.org.uk/portal/ukdebtaid_api.php?user_id=UKDE0059&user_token=UKDEB0D4E&user_pass=1111111’
let apiend = ‘&c_emp_status=NIL&industries=NIL&ref_page=ukdebtaid.co.uk&my_ip=1111111’ ;
let apiQuery = ‘’ ;
if ( $w ( “#fname” ). value !== null && $w ( “#fname” ). value . length > 0 ) { apiQuery += ‘&name=’ + $w ( “#fname” ). value + ‘%20’ + $w ( “#lname” ). value + ‘’ ; }
//if ($w(“#lname”).value !== null && $w(“#lname”).value.length > 0) {crmApiURL += ‘&name=[’+$w(“#fname”).value+‘]’;}
if ( $w ( “#phone” ). value !== null && $w ( “#phone” ). value . length > 0 ) { apiQuery += ‘&phone=’ + $w ( “#phone” ). value + ‘’ ; }
if ( $w ( “#email” ). value !== null && $w ( “#email” ). value . length > 0 ) { apiQuery += ‘&email=’ + $w ( “#email” ). value + ‘’ ; }
if ( $w ( “#radioGroup1” ). value !== null && $w ( “#radioGroup1” ). value ) { apiQuery += ‘&debts=’ + $w ( “#radioGroup1” ). value + ‘’ ; }
if ( $w ( “#radioGroup2” ). value !== null && $w ( “#radioGroup2” ). value ) { apiQuery += ‘&emp_status=’ + $w ( “#radioGroup2” ). value + ‘’ ; }
if ( $w ( “#radioGroup3” ). value !== null && $w ( “#radioGroup3” ). value ) { apiQuery += ‘&country=’ + $w ( “#radioGroup3” ). value + ‘’ ; }
console . log ( "1:" + crmApiURL )
console . log ( "2:" + apiQuery )
// etc. for each property
let fullurl = ‘https://www.nationaldebtassistance.org.uk/portal/ukdebtaid_api.php?user_id=UKDE0059&user_token=UKDEB0D4E&user_pass=1111111’ + apiQuery + ‘&c_emp_status=NIL&industries=NIL&ref_page=ukdebtaid.co.uk&my_ip=1111111’ ;
console . log ( "3:" + fullurl )
fetchMe ( fullurl ). then ( resp => { console . log ( "Response from fetch:" , resp ) })
}
/////Backend Code/////
import { fetch } from “wix-fetch” ;
export function fetchMe ( url ) {
return fetch ( url , { method : ‘post’ })
. then (( fetchResult ) => {
return fetchResult
})
. catch (( error ) => {
console . log ( error );
return error
})
}