How To Fetch Correct Option From a Radio Button? (SOLVED)

I want to pull the answer to a radio button question & match it to fields in an API post. Currently it’s posting the wrong answers as I haven’t indicated which option it should be fetching from the radio button… Well that is what I have figured anyway.

What should I be adding so it selects the correct answer from each of the 3 radio buttons?

import { local } from ‘wix-storage’ ;
$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=11111111
let apiend = ‘&c_emp_status=NIL&industries=NIL&ref_page=ukdebtaid.co.uk&my_ip=111111111’ ;

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 . length > 0 ) { apiQuery += ‘&debts=’ + $w ( “#radioGroup1” ). value + ‘’ ;}
if ( $w ( “#radioGroup2” ). value !== null && $w ( “#radioGroup2” ). value . length > 0 ) { apiQuery += ‘&emp_status=’ + $w ( “#radioGroup2” ). value + ‘’ ;}
if ( $w ( “#radioGroup3” ). value !== null && $w ( “#radioGroup3” ). value . length > 0 ) { apiQuery += ‘&country=’ + $w ( “#radioGroup3” ). value + ‘’ ;}

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’ ;

fetch ( fullurl , { method : ‘post’ })
// Check the result and manage errors - check the documentation for wix-fetch out
. then (( fetchResult ) => {
//Check to make sure the request was successful
})
. catch (( error ) => {
console . log ( error );
});}

instead of

if($w("#phone").value !==null&& $w("#phone").value.length >0)

You should do:

if($w("#phone").value)

and the same for the others.

They aren’t the problem, it’s only the radiogroups I need help with. Currently First name, last name, phone number & email get transferred to the clients CRM fine. I’ve got 3 questions that have 4 answers each which are answered by clicking a radio button, I need to be able to get the specific option for each radio button group.

@phil18799 instead of:

if($w("#radioGroup1").value !==null&& $w("#radioGroup1").value.length >0)

Use:

if($w("#radioGroup1").value)

//and for all the others

Explanation: Your code will throw an error if the radioButton value is undefined because undefined value doesn’t have a length property.
But my suggestion validates that there’s is a defined value that is not null and has a length of at least one character (because null, undefined, and empty strings are all not truthy).

@jonatandor35 Thanks I’ll give it a go soon & keep you posted.

@jonatandor35 Thank you this works.

Since I’ve used this code it no longer saves to the form database collection. How do I get that function back?

@phil18799 I think mixing code and editor action for the same element might be a problem.
Since you have code for the submit onClick it might collide with the dataset saving.
So you have 3 options (depends on what you need):

Instead of having the submitButton.onClick handler, put all this fetch inside:

  1. onBeforeSave( ) (i.e. run it before it saves to the dataset) OR:
  2. onAfterSave( ) (i.e. run it after saving to the DB) OR:

Use submitButton.onClick but:
3. Disconnect the Submit button from the dataset (on the editor) and save all the data using code: https://www.wix.com/velo/reference/wix-dataset/dataset/setfieldvalues , https://www.wix.com/velo/reference/wix-dataset/dataset/save

@jonatandor35 With some help from WIX support this has been sorted.

The fetch function needed moving to the backend.