Insert only the values of the selected checkboxes with their respective amounts into the Query String

Asked

Viewed 80 times

0

I’m having a small problem in mounting a Query String with the variables I need.
Well, I have the following code part:

echo "<form class='acrescimos'>";               
foreach($conexao->query($sql2) as $acrescimo)
{
    $preco_acrescimo_reajustado = number_format($acrescimo['preco_acrescimo'], 2, ",", ".");
    echo "
    <input class='caixa-acrescimo' type='checkbox' name='id_acrescimo' value='{$acrescimo['id_acrescimo']}'/>{$acrescimo['nome_acrescimo']} - R&#36;{$preco_acrescimo_reajustado} <input type='number' name='quantidade_acrescimo' value='1' min='1' max='5'/><br/>
    ";
}
echo "</form>";

In this code snippet I get the values of DB and insert each of them in a checkbox.
Now, to get the values of the selected checkboxes I use jQuery:

$(document).ready(function()
{
    $('.adicionar').on('submit', function()
    {
        var id_produto = $(this).closest('tr').find('.id-produto').text();
        var nome_produto = $(this).closest('tr').find('.nome-produto').text();
        var quantidade = $(this).closest('tr').find('input').val();
        if ($(this).closest('tr').find('input.caixa-acrescimo').is(':checked'))
        {
            var acrescimos = $(this).closest('tr').find( '.acrescimos' ).serialize();
            window.location.href = "adicionar.php?id_produto=" + id_produto + "&nome_produto=" + nome_produto + "&quantidade=" + quantidade + "&acrescimos=" + acrescimos;
        }
        else
        {
            window.location.href = "adicionar.php?id_produto=" + id_produto + "&nome_produto=" + nome_produto + "&quantidade=" + quantidade;
        }
        return false;
    });
});

Let’s assume only the value checkbox 1 is selected and in the input in front of its quantity the value is 3. Man output should be something like:

adicionar.php?id_produto=4&nome_produto=Pizza de Bacon&quantidade=1&acrescimos=id_acrescimo=1&quantidade_acrescimo=3

That’s where my little problem lies. I even have part of the output as I want, but the values of the other quantity inputs are also inserted when serializing, thus being:

adicionar.php?id_produto=4&nome_produto=Pizza de Bacon&quantidade=1&acrescimos=id_acrescimo=1&quantidade_acrescimo=3&quantidade_acrescimo=1&quantidade_acrescimo=1

How to make only quantity inputs for selected checkboxes in the Query String? Thank you!

  • I believe that when you are running the . serialize() function, it is returning several '.acrescimos' elements. I think one . filter() solves your problem. $(this). Closest('tr'). find('.acrescimos'). filter(Function(Indice, element){ /* Put a logic here. */ Return true; }). serialize();

  • @Wédneyyuri This seems to be the way, but I don’t know how to relate the input of the checkbox to the quantity input so I can use this function to obtain the quantities only of the additions that are selected.

No answers

Browser other questions tagged

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