1 votes

Error button status change

I have a table called user which contains one of its fields called status, I use this field inside an administrator panel which when I press the button should change the status that I have defined as inhabilitado=0 y habilitado=1 My problem is that it changes the status only when the user has status 1 and I need it to work both ways because the administrator needs to enable the users that he wants so that they can access the system. Attached is the code.

$(document).ready(function name(params) {
    console.log('Jqueri is working');

    $(document).on('click', '.changeStatus', function (e) {
     console.log("this", $(this)); //obtienes el bottom que fue clickeado
    let element = $(this)[0];
    let status = $(element).attr('buttonStatus');

    console.log('primer', status);
    status = !status;
    console.log('segundo', status);
    const id = $(element).attr('buttonId');
    $.post('../controllers/changeStatus.php', {id: id , status: status}, function(response) {
        console.log(response); 
    })

    });
}); 

<?php
                                require_once '../DBconect.php';

                                $select_stmt=$db->prepare("SELECT id,name,email,role,status FROM user");
                                $select_stmt->execute();

                                while($row=$select_stmt->fetch(PDO::FETCH_ASSOC))
                                {

                                    $status=$row['status'];
                                    $color = 'success';
                                    $title = 'Habilitar';
                                    if($status==1) {
                                        $color = 'danger';
                                        $title = 'Inhabilitar'; 
                                    }
                                    $class_name="btn btn-lg btn-$color";

                                ?>

                                    <tr>
                                        <td><?php echo $row["id"]; ?></td>
                                        <td><?php echo $row["name"]; ?></td>
                                        <td><?php echo $row["email"]; ?></td>
                                        <td><?php echo $row["role"]; ?></td>
                                        <td>*******</td>
                                        <td width="1%"><button class="changeStatus <?php echo $class_name; ?>" buttonId="<?php echo $row["id"]; ?>" buttonStatus="<?php echo $status; ?>"><span class="glyphicon" aria-hidden="true"><?php echo $title ?></span></button></td>

                                    </tr>

0voto

Felipe Urcia Points 31

I was referring to the fact of obtaining the data from the input, this comes as text, if you change 'true' it will change to false, but 'false' will not change to true, because you obtain a text, if you wish you can use the following if ternary:

status = status == 'true' ? false : true;

Additionally, I am resetting the value of the buttonStatus The shipping value will be the opposite when you reship the product.

$(document).ready(function name(params) {
  console.log('Jqueri is working');

  $(document).on('click', '.changeStatus', function(e) {
    let element = $(this)[0];
    let status = $(element).attr('buttonStatus');

    console.log('primer', status);
    status = status == 'true' ? false : true;
    $(element).attr('buttonStatus', status);
    console.log('segundo', status);
    const id = $(element).attr('buttonId');
    $.post('../controllers/changeStatus.php', {
      id: id,
      status: status
    }, function(response) {
      console.log(response);
    })

  });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<td width="1%"><button class="changeStatus 1" buttonId="1" buttonStatus="false"><span class="glyphicon" aria-hidden="true">Cambia de status</span></button></td>

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X