Update database with form data without refresh

0

Hello, I'm doing a control panel of a scoreboard, where I have a screen with all the games of the round and since the games have already been registered in the bank, I need to update the games as the rounds occur. I managed to pass the ID of the game, but the other inputs with the results did not.

PHP

<?php
while($ln = mysqli_fetch_assoc($qr)){
?>
    <form id="frmPlacar" method="post"> 
    <TABLE align="center">
        <TR>
            <TH rowspan="2">
                <INPUT type="checkbox" name="ids[]" id="ids" value="<?php echo $ln['id_placar'];?>">
            </TH>
            <TH>
                <INPUT type="text" name="time1" value="<?php echo $ln['nm_time1'];?>" placeholder="Time visitante" disabled>
            </TH>
            <TD>
                <INPUT type="text" name="placar1_1" value="<?php echo $ln['nr_placar1_1'];?>" placeholder="1&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar1_2" value="<?php echo $ln['nr_placar1_2'];?>" placeholder="2&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar1_3" value="<?php echo $ln['nr_placar1_3'];?>" placeholder="3&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar1_4" value="<?php echo $ln['nr_placar1_4'];?>" placeholder="4&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
               <INPUT type="text" name="placar1_o" value="<?php echo $ln['nr_placar1_o'];?>" placeholder="OT" maxlength="2">
            </TD> 
            <TD>
                <INPUT type="text" name="placar1_f[]" value="<?php echo $ln['nr_placar1_f'];?>" placeholder="FINAL" maxlength="2">
            </TD> 
            <TD>
                <INPUT type="text" name="campanha1" value="<?php echo $ln['nm_campanha1'];?>" placeholder="CAMPANHA" maxlength="7">
            </TD> 
        </TR>
        <TR>
            <TH>
                <INPUT type="text" name="time2" value="<?php echo $ln['nm_time2'];?>" placeholder="Time mandante" disabled>
            </TH>
            <TD>
                <INPUT type="text" name="placar2_1" value="<?php echo $ln['nr_placar2_1'];?>" placeholder="1&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar2_2" value="<?php echo $ln['nr_placar2_2'];?>" placeholder="2&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar2_3" value="<?php echo $ln['nr_placar2_3'];?>" placeholder="3&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
                <INPUT type="text" name="placar2_4" value="<?php echo $ln['nr_placar2_4'];?>" placeholder="4&ordm; Q" maxlength="2">
            </TD>                                    
            <TD>
               <INPUT type="text" name="placar2_o" value="<?php echo $ln['nr_placar2_o'];?>" placeholder="OT" maxlength="2">
            </TD> 
            <TD>
                <INPUT type="text" name="placar2_f[]" value="<?php echo $ln['nr_placar2_f'];?>" placeholder="FINAL" maxlength="2">
            </TD> 
            <TD>
                <INPUT type="text" name="campanha2" value="<?php echo $ln['nm_campanha2'];?>" placeholder="CAMPANHA" maxlength="7">
            </TD> 
        </TR>
        <TR>
            <TD colspan="8" class="bt" align="right">
                <input type="hidden" value="<?php echo $ln['id_placar'];?>" name="jogos[]" id="jogo">
                <button class="btn btn-small btn-success" id="botao"><i class="icon-thumbs-up icon-white"></i>Alterar</button>
            </TD> 
        </TR>
        <TR>
            <TD colspan="8" align="left">
                <div id="erro">Erro:</div>
                <div id="sucesso">Sucesso:</div>
            </TD>
        </TR>
    </TABLE>
    </form>
<?php
}
?>  

AJAX

$(document).ready(function(){
    $(document).on('click', '#botao', function () {

        var Jogo = new Array();

        $("input[name='ids[]']:checked").each(function(){
            Jogo.push(parseInt($(this).val()));
            alert(Jogo);
        });

        $.ajax({
            type: "POST",
            url: "teste.php",
            data: "jogo="+Jogo,
            success: function(html){
                if(html=='true')
                {
                    $("#erro").html("");
                    $("#sucesso").html("Placar atualizado com sucesso!");
                }
                else
                {
                    $("#erro").html(html);
                    $("#sucesso").html("");
                }
            },
            beforeSend:function()
            {
                $("#erro").html("");
                $("#sucesso").html("");
            }
        });
        return false;
    })
})
    
asked by anonymous 24.05.2016 / 20:48

1 answer

1

The error is that you are only removing the value of input whose name=ids[] e is if it is checked .

The error is in this line:

$("input[name='ids[]']:checked").each(function(){
   ...

You can change to:

$('#frmPlacar input:checked').each(function(){
   ...

You'll already get the value of all inputs that are checked

Why are you passing the values as an array to the server? Were you working on the teste.php file? If it was and I'm allowed a suggestion, I suggested that it be passed as Object, as array might not give the expected result:

I would do without the array Jogo :

var dados = {};
$('#frmPlacar input:checked').each(function(){
    // aqui cada chave vai ser o 'name' do input
    dados[$(this).prop('name')] = $(this).val();
});

...
data: dados,
success: function(html){
   console.log(html);
   ...

test.php

if($_SERVER['REQUEST_METHOD'] == 'POST') {

    print_r($_POST);
    // verificar consola para ver resposta, se resulta
    // depois para aceder a cada key vá pelo 'name' dos inputs, não esquecendo que alguns deles são arrays. ex:
    print_r($_POST['placar2_f']); 
}
    
24.05.2016 / 22:28