doubt javascript function

Asked

Viewed 78 times

0

I have a page with a table and data coming from the database, 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 it is inserted in the BD it goes as available, in this same column I added a button to change the status to: pending and collected, but my javascript function is not working, which is wrong?

getdados file.php

<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 
   } 
?>   

php status 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{           

   }

?>    
  • Even call the url, status.php? Guy I think one is missing WHERE, in your SQL, this is not what is causing it to fail, but the way it is will update in your entire table.

  • yes I called him in getdados.php, where do I put the function? in the file that is the table? or in getdados msm?

  • Set "not working". What happens? Gives error ? Confirm on browser inspection. If yes what error appears ? Take advantage and also confirm in the network tab of the inspect if the orders are being made and what data has in them.

  • not changing the status in the bank, my doubt and the following, I would just like to know if the function is right, if I am passing the value correctly to the function, if I have to pass the id of the record I will change, as I pass to the function.

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',
        });
    });
});

php status.

    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 have only one ' [...]status = $escolha', status is a reserved word too. right would be [...] `status` = '$escolha'

  • when I hover over the console option returns following error: [Violation] Added non-passive Event Listener to a scroll-blocking 'mousewheel' Event. Consider marking Event Handler as 'Passive' to make the page more Responsive.

  • Are you using a framework other than jQuery for javascript? something else tbm, you can’t use <select id='sel1'> because id has to be a unique value on your html page

  • I am using bootstrap, so how do I pass the data from the button to the javascript event from another page?

Browser other questions tagged

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