Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
How to push radio data in form submit

I'm using this code to send data to my sheet.

For The FullName, Phone, Address, ..., data is sent with no problem.

But I'm blocked to split and push value input radio data and send it to my sheet.

Example if the second input of radio is selected ( sku_order_2|quantity2|price22 )

sku = sku_order_2 quantity = quantity2 price = price22

const scriptURL = 'https://script.google.com/........'
    const form = document.forms['formName']

    form.addEventListener('submit', e => {
      e.preventDefault()
      fetch(scriptURL, { method: 'POST', body: new FormData(form)})
        .then(response => console.log('Success!', response))
        .catch(error => console.error('Error!', error.message))
    })
<form action="" name="formName" method="post" id="formName" data-redirect="" class="form">


<input id="order1" class="variant" type="radio" name="order" value="sku_order_1|quantity1|price15" hidden="" checked="">
<input id="order2" class="variant" type="radio" name="order" value="sku_order_2|quantity2|price22" hidden="">
<input id="order2" class="variant" type="radio" name="order" value="sku_order_3|quantity3|price26" hidden="">



<input id="fullname" class="input" type="text" placeholder="Fullname" name="fullname" required="">
<input id="phone" class="input" type="number" placeholder="Phone" name="phone" required="">
<input id="address" class="input" type="text" placeholder="Address" name="address" required="">

</form>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use the FormData methods (https://developer.mozilla.org/en-US/docs/Web/API/FormData)

You can get the order value with formData.get('order')

than you can split (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) the string and use the result to append the fields: sku, quantity, price.

const scriptURL = 'https://script.google.com/........'
    const form = document.forms['formName']
    form.addEventListener('submit', e => {
      e.preventDefault()
      const formData = new FormData(form)
      const order = formData.get('order').split('|') // Split the string using '|' into an array [sku, quantity, price]
      formData.append('sku', order[0])
      formData.append('quantity', order[1])
      formData.append('price', order[2])
      formData.delete('order')
      fetch(scriptURL, { method: 'POST', body: formData})
        .then(response => console.log('Success!', response))
        .catch(error => console.error('Error!', error.message))
    })
<form action="" name="formName" method="post" id="formName" data-redirect="" class="form">

<div>
    <input id="order1" class="variant" type="radio" name="order" value="sku_order_1|quantity1|price15" checked=""> 1
    <input id="order2" type="radio" class="variant" name="order" value="sku_order_2|quantity2|price22"> 2
    <input id="order3" class="variant" type="radio" name="order" value="sku_order_3|quantity3|price26"> 3
</div>


<input id="fullname" class="input" type="text" placeholder="Fullname" name="fullname" required="">
<input id="phone" class="input" type="number" placeholder="Phone" name="phone" required="">
<input id="address" class="input" type="text" placeholder="Address" name="address" required="">
<input type="submit" value="Send">
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to split the order property, you can do the following:

  1. Get the order values from formData
  2. Split the retrieved order value by separator |
  3. Map the exploded order values to their representatives sku, quantity and price
  4. add sku, quantity and price to formData

See this fiddle: https://jsfiddle.net/Lxujkotp/1/

Extended Code:

form.addEventListener('submit', e => {
    var data = new FormData(form);
    var order = data.get('order');
    var orderParts = order.split('|');

    // WARNING: if the "order values" does not have a fix ordering, map the values here more sophisticated!
    var sku = orderParts[0];
    var quantity = orderParts[1];
    var price = orderParts[2];


    e.preventDefault();

    data.delete('order');

    // approach 1: plain
    data.set('sku', sku);
    data.set('quantity', quantity);
    data.set('price', price);

    // alternative approach 2: nested
    // data.set('order', JSON.stringify({
    //     sku: sku,
    //     quantity: quantity,
    //     price: price
    // }));

    fetch(scriptURL, { method: 'POST', body: data })
        .then(response => console.log('Success!', response))
        .catch(error => console.error('Error!', error.message))
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda