Table html + js + golang

91 views
Skip to first unread message

Wellington Arantes

unread,
Apr 2, 2023, 8:35:54 PM4/2/23
to golang-nuts
Hello,

I'm a noob about Golang, I'm studying and making samples...
I have a form HTML with inputs and a table
I created a function to update and execute a function javascript
In function, js get all information from HTML, inputs, and table, but in golang
I don't get the table information...How can I do it?

Function Golang

func EditarCondicaoPagamento(w http.ResponseWriter, r *http.Request) {
    //para post, put usase o ParseForm para ler os dados da pagina html
    r.ParseForm()

    //r.FormValue("nome do campo no html") le o valor do campo no form html

    codCondicaoPagamento, _ := strconv.ParseUint(r.FormValue("codCondicaoPagamento"), 10, 64) //pega o codigo do servico que está no form html

    //le os valores do formulario html
    condicaoPagamento, erro := json.Marshal(map[string]interface{}{
        "codCondicaoPagamento":      codCondicaoPagamento,
        "nome":                      r.FormValue("nome"),
        "numParcelas":               r.FormValue("numParcelas"),
        //"codFormaPagamento":         r.FormValue("codFormaPagamento"),    
        "parcelas":                  r.FormValue("parcelas"),
    })
   

    fmt.Println(bytes.NewBuffer(condicaoPagamento))

I don't get array value "parcelas"

Result:
{"codCondicaoPagamento":4,"nome":"ENTRADA + 1","numParcelas":"2","parcelas":""}

Function JS

function editarCondicoesPagamento(evento){
    evento.preventDefault();

    var parcelasTabela = [];

    var lista = $('#dtcondicoesPagamentoParcelamento');  

    $(lista).find("tr").each(function(index, tr) {
        parcelasTabela.push(JSON.stringify({"codCondicaoPagamentoParc": $(tr).find('td:eq(0)').html(),
                                            "numParcela"              : $(tr).find('td:eq(1)').html(),
                                            "dias"                    : $(tr).find('td:eq(2)').html()}))

    });
    parcelasTabela.shift();

    $.ajax({
        url : `/editar-condicoespagamento`,
        method : "PUT",
        data : {
            codCondicaoPagamento     : $( "#codCondicaoPagamento" ).val(),
            nome                     : $( "#nome" ).val(),
            numParcelas              : $( "#numParcelas" ).val(),
            parcelas                 : parcelasTabela,  
        }      
    }).done(function (){
       Swal.fire("Sucesso","Dados Atualizados com Sucesso!","success")
       /*.then(function(){
         window.location = "/editar-cliente";//perfil
       });*/
    }).fail(function() {
      Swal.fire("Ops...","Erro ao Atualizar!","error")
    });
}

Thanks!

Brian Candler

unread,
Apr 3, 2023, 4:08:17 AM4/3/23
to golang-nuts
I suggest you look at the actual request body which the client is sending, using your browser's developer console (or at worst using tcpdump/wireshark, as long as it's not HTTPS).

Then it should be clear whether your Javascript is sending a "parcelas" form value, and if so what it contains.  I note that you've passed a Javascript array (parcelasTabela) without JSON.stringify'ing it, so you'll have to see what it actually sends, if anything. My guess is that your issue is with the Javascript, not the Go.

Given that this is a complex structured value, it might be better to make the whole request use JSON instead of form data though.
Reply all
Reply to author
Forward
0 new messages