user3658717
user3658717

Reputation: 41

Increment counter on DataBase using jquery

I have a field on my database that stores a number of clicks, and want to increment that when I click in a link(<a href="#selecoes" data-identity="31" id="clicks" clicks="0">) of my tag cloud. Note that I get the number of clicks throught my webservice. This is I do so far:

index.html

<div id="tags">
      <ul id="tagList">
       <li>
           <a href="#selecoes" data-identity="31" id="clicks" clicks="0"><img src='.../>Brasil</a>
       </li>                
</ul>

main.js

    $('#tagList a').live('click', function() {
        findByIdSelecoes($(this).data('identity'));
    });

    function findByIdSelecoes(id) {
        console.log('findByIdSelecoes: ' + id);
        $.ajax({
            type: 'GET',
            url: rootURLSelecoes + '/id/' + id,
            dataType: "json",
            success: function(data){
                $('#btnDelete').show();
                console.log('findByIdSelecoes success: ' + data.nome);
                currentWine = data;
                renderDetails(currentWine);
                findJogadoresBySelecao(id);
                addClick(currentWine);

            }
        });
    }


function addClick(selecao) {
    console.log('addClick na seleção: ' + selecao.id_selecao);
    $.ajax({
        type: 'PUT',
        contentType: 'application/json',
        url: rootURLSelecoes + '/update/' + selecao.id_selecao,
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert('clicks updated successfully');


        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('updateWine error: ' + textStatus);
        }
    });
}



function formToJSON() {
    return JSON.stringify({
        "clicks": ($('#clicks').val())++ // i dont know what i have to do so i try this(don't work)
        });
}

I can't update the dataBase when I click the link in the list. The function formToJSON doesn't increment the value in the database.

Upvotes: 1

Views: 631

Answers (1)

seal
seal

Reputation: 520

Try to do this

Make a var number like global, and do this:

function addClick(selecao) {
    console.log('addClick na seleção: ' + selecao.id_selecao);
    number = parseInt(selecao.clicks,10);
    $.ajax({
        type: 'PUT',
        contentType: 'application/json',
        url: rootURLSelecoes + '/update/' + selecao.id_selecao,
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert("Done: " +number);




        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('updateWine error: ' + textStatus);
        }
    });
}


function`enter code here` formToJSON() {
    var ola = parseInt(number,10);
    ola = ola +1;
    return JSON.stringify({
        "clicks": parseInt(ola,10)
        });
}

Upvotes: 1

Related Questions