0

I've created a table where my data can be ACTIVE or INACTIVE and this is already working, my goal is I just want to add a function that when I clicked the button to make my data INACTIVE, it will pop up a modal and has a message. But I don't know how will I make it pop up.

I've found a similar post but it cannot be applied to mine, because what I did has a different approach and I think mine is easier for myself to understand and will or might complicate things for me if I try to use the link. Thanks in advance

Here is my code in my JQUERY

$(document).on('click', '.status_checks', function() {

  var status = '1';
  var selector = $(this);
  var id = $(this).data('id');

  if ($(this).hasClass("btn-success")) {
    status = '0';
  }

  $.ajax({
    type: "POST",
    url: "../forms/form_BtnStats.php",
    data: {
      id: id,
      status: status
    },
    success: function(data) {
      selector.hasClass("btn-success") ? (selector.removeClass("btn-success").addClass(
          "btn-danger"),
        selector.text("Inactive")) : (selector.removeClass("btn-danger").addClass(
          "btn-success"),
        selector.text("Active"));
      // location.reload();
    }
  });

});

// For status Active/Inactive
function StatusChange() {


  $("#dataTables").DataTable().destroy();
  $("#tb_body").empty();

  var status = $('#stats').val();

  if (status) {

    $.ajax({
      type: "POST",
      url: "../forms/form_statusForspecs_dtbl.php",
      data: {
        status: status
      },
      success: function(data) {

        $("#dataTables").append(data);
        $("#dataTables").DataTable();

      }
    });
  }
}

// Startup
$(document).ready(function() {
  $('#stats').change(function() {
    $(this).val();
  });

  $("#stats").trigger('change');
});
@import https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <style>
    .statusButton {
        color: white;
        display: inline-block;
        margin-bottom: 0;
        font-weight: 400;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        background-image: none;
        border: 1px solid transparent;
        border-radius: 20px;
        white-space: nowrap;
        padding: 2px 12px;
        font-size: 12px;
        line-height: 1.42857143;
        border-radius: 4px;
        user-select: none;
    }
    </style>
</head>
<!-- this is the modal that I want to pop up -->
<div class="modal fade" id="reason_for_modal">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <center> Confirmation </center>
        </h4>
      </div>

      <!-- Modal body -->
      <div class="modal-body panel-body">
        <center>
          <br><br>
          <p> <strong> Reason for Inactive </strong> </p>
        </center>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <div class="col-sm-15">
          <div class="form-group">
            <button type="submit" id="submitBtn" class="btn btn-danger pull-right" data-toggle="modal" data-target="#maModal" onclick="window.location.href='#'">
                                <span class="fa fa-check"></span>
                                Close
                            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



<div id="page-wrapper">
  <div class="row">
    <div class="col-lg-12">
      <br>
      <br>
      <a href="../forms/form_toolspec.php">
        <button class="btn btn-primary pull-right">
                            <span class="fa fa-plus-circle"></span>
                            Add Record
                        </button>
      </a>
      </h1>
    </div>
    <!-- /.col-lg-12 -->
  </div>
  <br>
  <!-- Status -->
  <div>

    <form action="GET">
      <div class="form-group">
        <label> Table group by Status </label>
        <select name="selector_id" onchange="StatusChange()" style="width:200px;" class="show-menu-arrow form-control" id="stats">
          <!-- <option value="" disabled> Select Status </option> -->
          <option value="1" selected> ACTIVE </option>
          <option value="0"> INACTIVE </option>
        </select>
      </div>
    </form>
  </div>

  <!-- /.row -->
  <div class="row">
    <div class="col-lg-12">
      <div class="panel panel-primary">
        <div class="panel-heading">
          Table of Tools Specification
        </div>
        <!-- /.panel-heading -->
        <div class="panel-body" id="showStatus">
          <div class="table-responsive">
            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables">
              <thead>
                <tr>
                  <th> Tools Name </th>
                  <th> Model No. </th>
                  <th> Value </th>
                  <th> Number of days </th>
                  <th>
                    <center> Status </center>
                  </th>
                </tr>
              </thead>
              <tbody id="tb_body">
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Here is my form_statusForspecs_dtbl.php AJAX call in function StatusChange()

<?php
include ("../include/connect.php");
 
$status = $_POST['status'];
$con->next_result();
$result = mysqli_query($con, "CALL GetToolSpecByStatus('$status')");  
 while ($row = mysqli_fetch_assoc($result)) 
 {
   echo '<tr>';
   echo "<td><a href='edit_toolspec.php?ID=".$row['ID']."' style='color:red;'>" . $row['tools_name'] . "</a></td>";  
   echo '<td>'.$row['model_num'].'</td>';  
   echo '<td>'.$row['model_num_val'].'</td>';  
   echo '<td>'.$row['no_of_days'] .'</td>';  ?>

   <td>
    <center>
        <p data-id="<?php echo $row['ID'];?>"
            class="status_checks statusButton <?php echo ($row['status'])? 'btn-success': 'btn-danger'?>">
            <?php echo ($row['status'])? 'Active' : 'Inactive'?>
        </p>
    </center>
</td>
<?php 
   echo '</tr>';
 }

 ?>

Here is my form_btnStats.php AJAX call for button to change status and will be recorded in sql database

<?php 

include('../include/connect.php');

$user_id=$_POST['id'];
$newStatus=$_POST['status'];
$query = "UPDATE tools_spec SET status='$newStatus' WHERE ID = '$user_id'";
$result = mysqli_query($con, $query);
    if($result === TRUE)
    {
        echo json_encode(1);
    }
    else
    {
        echo json_encode(0);
    }
?>
  • Did you try to use CSS and just change 'display' property (for example by adding/removing class) ? – jaromir Feb 05 '21 at 00:35
  • Yes sir I did use CSS –  Feb 05 '21 at 00:38
  • Editted it and inserted my CSS @jaromir –  Feb 05 '21 at 00:52
  • So I assume it is not working ? I cannot see anything in your 'StatusChange' function that would change the state of your modal. Like `if(status == 0) { jQuery('modal-selector').show(); }`. – jaromir Feb 05 '21 at 00:53
  • It's working, it is because the table is inside and my ajax, jquery will be responsible on showing it. I should edit it again and show my two ajax. –  Feb 05 '21 at 01:13
  • Hi so you need just need to show some pop up where user will click yes/no and depending on that your status will update ? – Swati Feb 05 '21 at 04:09
  • Hi @Swati! yes that is what I need. But the pop up will just show if you will make the data inactive. –  Feb 05 '21 at 04:12

2 Answers2

0

Option 1: Simple CSS class assignment

Example: https://www.w3schools.com/w3css/w3css_modal.asp

Option 2: Use jQuery.show() / jQuery.hide()

function StatusChange() {
   ...
   if (status == 0) {
      // This should use # / id
      jQuery('div.modal').show();
   }
}

Option 3: Use jQuery UI dialog

Example: https://jqueryui.com/dialog/
jaromir
  • 91
  • 3
0

As you only need to show popup when making button inactive you add code to show modal inside if(status == '1'){ and use $("#reason_for_modal").modal('show'); to show same . Also , put some condition around your ajax call so that it will execute only when status is 1 .Nextly ,if user click on deactivate button inside modal you can create one click event handler for same and pass value to ajax from there.

Demo Code :

var id;
$(document).on('click', '.status_checks', function() {

  var status = '1';
  var selector = $(this);
  id = $(this).data('id');
  if ($(this).hasClass("btn-success")) {
    status = '0'; //change to 0
    $("#reason_for_modal").modal('show'); //show modal
  }
  //enter only when status is 1
  if (status == '1') {
    /*$.ajax({
      type: "POST",
      url: "../forms/form_BtnStats.php",
      data: {
        id: id,
        status: status
      },
      success: function(data) {*/
    selector.removeClass("btn-danger").addClass(
      "btn-success");
    selector.text("Active");

    /*}
  });*/
  }
});
//if activate btn is click
$("#submitBtn").on("click", function() {
  var reason = $("#reason").val(); //get reason..
  $("#reason_for_modal").modal('hide'); //hide modal
  console.log(id)
  $("#reason").val("")//empty textarea
  /*$.ajax({
      type: "POST",
      url: "../forms/form_BtnStats.php",
      data: {
        id: id,
        status: 0,
        reason:reason
        
      },
      success: function(data) {*/
  //change class and text
  $("p[data-id=" + id + "]").removeClass("btn-success").addClass("btn-danger")
  $("p[data-id=" + id + "]").text("Inactive")
  /*}
  });*/

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<!-- this is the modal that I want to pop up -->


<!-- /.row -->
<div class="row">
  <div class="col-lg-12">
    <div class="panel panel-primary">
      <div class="panel-heading">
        Table of Tools Specification
      </div>
      <!-- /.panel-heading -->
      <div class="panel-body" id="showStatus">
        <div class="table-responsive">
          <table width="50%" class="table table-striped table-bordered table-hover" id="dataTables">
            <thead>
              <tr>
                <th> Tools Name </th>
                <th> Model No. </th>
                <th> Value </th>
                <th> Number of days </th>
                <th>
                  <center> Status </center>
                </th>
              </tr>
            </thead>
            <tbody id="tb_body">
              <tr>
                <td><a href="edit_toolspec.php?ID=1" style="color:red;">abc</a></td>
                <td>14</td>
                <td>1</td>
                <td>5</td>

                <td>
                  <center>
                    <p data-id="1" class="status_checks statusButton btn-success">
                      Active
                    </p>
                  </center>
                </td>
              </tr>
              <tr>
                <td><a href="edit_toolspec.php?ID=2" style="color:red;">abc1</a></td>
                <td>14</td>
                <td>11</td>
                <td>51</td>

                <td>
                  <center>
                    <p data-id="2" class="status_checks statusButton btn-danger">
                      Inactive
                    </p>
                  </center>
                </td>
              </tr>
              <tr>
                <td><a href="edit_toolspec.php?ID=3" style="color:red;">ab3</a></td>
                <td>13</td>
                <td>13</td>
                <td>51</td>

                <td>
                  <center>
                    <p data-id="3" class="status_checks statusButton btn-success">
                      Active
                    </p>
                  </center>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="reason_for_modal">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <!-- Modal Header -->
      <div class="modal-header">
        <center> Confirmation </center>
      </div>
      <!-- Modal body -->
      <div class="modal-body panel-body">
        <center>
          <br><br>
          <p> <strong> Reason for Inactive </strong> </p>
          <textarea id="reason"></textarea>
        </center>
      </div>
      <!-- Modal footer -->
      <div class="modal-footer">
        <div class="col-sm-15">
          <div class="form-group">
            <button type="button" id="submitBtn" class="btn btn-success pull-right"> De-Activate</button>
            <button type="button" class="btn btn-danger pull-right" data-toggle="modal" data-dismiss="modal">
               <span class="fa fa-check"></span>
               Close
               </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Hi @Swati I have a question again, I have trouble on saving my reason message to my ajax. When I tried to update my other files it send an error and undefines other values –  Feb 05 '21 at 07:06
  • Hi ,do `console.log(reason)` inside event handler see if its showing correct value . – Swati Feb 05 '21 at 07:09
  • Yeah thanks I got it's value. One last thing is I created a column inside my datatable named reason. Now if Inactive it can be viewed but active it should not. I tried to hide it but my code is not working how should I do it? :( –  Feb 05 '21 at 07:26
  • 1
    check [this](https://stackoverflow.com/questions/5654633/jquery-datatables-hide-column) might help. – Swati Feb 05 '21 at 07:37