doubts javascript function

0

I have a page with a table and data coming from the bank, this page has automatic refresh, the data is coming from another page (getdados.php), this table has a column called status, which by default when inserted in BD it goes as available, in this same column I added a button to change the status to: pending and collapsed, but my javascript function is not working, what's wrong?

getdados.php file

<script>
         $(document).ready(function(){
             $('#sel1').on('change', function(){
              var selecionado = $('#sel1').val();
          $.ajax({
           data: 'selecionado='+selecionado,
          type: 'post',
         url: 'status.php', 
         });
     });
 });

<?php
//Conectando ao banco de dados
$con = new mysqli("localhost", "root", "", "tcc");
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

//Consultando banco de dados
$qryLista = mysqli_query($con, "SELECT * FROM postagens");    
while($r = mysqli_fetch_assoc($qryLista)){
?>
<table id='tabela'>
<tr>
<td><?= $r['id'] ?></td>
<td><?= $r['nome'] ?></td>
<td><?= $r['rua'] ?></td>
<td><?= $r['bairro'] ?></td>
<td><?= $r['telefone'] ?></td>
<td><?= $r['descricao'] ?></td>     
<td>
<select id='sel1'>
<option value='status'><?= $r['status'] ?></option>
<option value='pendente'>Pendente</option>
<option value='recolhido'>Recolhido</option>
</select></td>
</table>

<?php 
   } 
?>   

status.php file

   <?php
    include_once("setting.php");
   $escolha = filter_input(INPUT_POST,'selecionado');

   if($escolha === 'recolhido'){
    $sql = "UPDATE postagens  SET status = $escolha'";
    $res = mysqli_query($conn, $sql);
    $linhas = mysqli_affected_rows($conn);
    echo $linhas;
    if ($linhas ==1){   
    echo "sucesso";
        }else{
            echo "erro";
        }   

  }else{           

   }

?>    
    
asked by anonymous 12.09.2017 / 04:31

1 answer

0

Your javascript function is correct. You need to send an id to update only that record in the database.

getdados.php

<?php
//Conectando ao banco de dados
$con = new mysqli("localhost", "root", "", "tcc");
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

//Consultando banco de dados
$qryLista = mysqli_query($con, "SELECT * FROM postagens");    
?>
<table id='tabela'>
    <?php while($r = mysqli_fetch_assoc($qryLista)){ ?>
        <tr>
            <td><?= $r['id'] ?></td>
            <td><?= $r['nome'] ?></td>
            <td><?= $r['rua'] ?></td>
            <td><?= $r['bairro'] ?></td>
            <td><?= $r['telefone'] ?></td>
            <td><?= $r['descricao'] ?></td>     
            <td>
                <select class="sel" data-id='<?= $r['id'] ?>'>
                    <option value='status'><?= $r['status'] ?></option>
                    <option value='pendente'>Pendente</option>
                    <option value='recolhido'>Recolhido</option>
                </select>
            </td>
        </tr>
    <?php }  ?>
</table>

$(document).ready(function () {
    $('.sel').on('change', function () {
        var selecionado = $(this).val(); 
        var id = $(this).attr('data-id');
        $.ajax({
            data: "id="+ id +"&selecionado=" + selecionado,
            type: 'post',
            url: 'status.php',
        });
    });
});

status.php     

    if($escolha === 'recolhido'){
        $sql = "UPDATE 'postagens' SET 'status' = '$escolha' WHERE 'id' = '$id'";
                    // ^ Sempre faça o escape para você não ter problemas com variáveis internas.
        $res = mysqli_query($conn, $sql);
        $linhas = mysqli_affected_rows($conn);
        echo $linhas;
        if ($linhas == 1){   
            echo "sucesso";
        }else{
            echo "erro";
        }   

    }else{           

    }
?>

In your original query, you only have ' [...]status = $escolha' , status is a reserved word as well. the right one would be [...] 'status' = '$escolha'

    
12.09.2017 / 13:44