Show selected checkbox number taking into account existing records in the database

Asked

Viewed 106 times

0

Hello I have a script that updates the database every time I check the checkbox without the client seeing;

        <script> 
        $(document).ready(function() { 
            $('.check').click( function () { 
                if ($(this).is(':checked')) { 
                    $.post("altera.php", { checado : '0', id: $(this).val() }, function () { 
                    }); 
                } else { 
                    $.post("altera.php", { checado : '1', id: $(this).val() }, function () { 

                    }); 
                } 
            }); 
        }); 
    </script>

and another counting the checkbox number the customer has already selected;

        var contador = function() {
        var n = $("input:checked").length;
        $(".checkcount").text((n === 0 ? "" : "Já foram selecionadas ") + n + (n === 1 ? " foto" : " fotos") + "" );
        };
        contador(); 
        $( "input[type=checkbox]" ).on( "click",contador);

the problem is that I could not make this second code take into account the first one because when I select the checkbox on the same page it counts normal but when I change page it shows zeroed because it does not consider the checkbox that has already been marked in the database by first code;

  • Do you have a server function that gives you the selected checkbox number? Otherwise you can put it on Session or go through page-to-page URL.

  • I would do following @Sergio’s reasoning on the return of his POST you inform how many records are selected in the database.

  • Thanks, @Maicon the idea would be in alterphp check the quantity and return but how I would do it I do not know,

  • I was able to create the Session and be updated every time I click but I can’t use that Session in the account of the second script that checks how many have already been checked; could help me;

  • @Sergio, I created Session and I’m trying to use it as follows ' var banco = <?php echo $_SESSION['totalfotos']; ? >; var a = $("input:checked"). length; if (bank === 0) { //Alert (zeroed); var n = parseint($("input:checked").length)- parseint(bank); } Else { //Alert(jatem); var n = parseint($("input:checked").length) + parseint(bank); }' but he always adds instead of only considering that that number is a preserver already

  • @Paulojorge what gives you alert(banco + ' ' + typeof banco);?

  • @Sergio I did the following after the line $. post("altera.php", { checado : '0', id: $(this).val() }, Function () { //Alert ('selected'); }); $("#tags"). load("account.php"); I put this . load to call this php that counts the records and returns me how many have already been chosen, I do not know if it is the best but it is what solved me

  • @Paulojorge sounds good, you could put an answer to that. I think you could do that consulate right when you click the page and put it in a variable? Put your answer. If you put the PHP code together with my idea.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.