-1

Currently, I created a system that has AJAX function. To be more clear, below is my current process flow:

1) dashboard.php will display 3 select option which is team, time from and time to

2) user need to complete all 3 select option and click button 'search'. At this point where AJAX (range.php).

3) All data row will be listed and each data have a delete button. User can delete data based on data row.

My problem is, the data is not deleted.

Below is my current code.

dashboard.php

      <select class="form-control"  name="team" id="team">
        <option value="">Please select...</option>
        <?php foreach ($data as $row2): ?>
        <option value= <?php echo $row2["team_id"]; ?> <?php echo (($_GET["team"] ?? '') == $row2["team_id"]) ? 'selected' : ''; ?> ><?php echo $row2["fullname"]; ?></option>
        <?php endforeach ?>
      </select>
    <td width="1%"></td>
    </td>
    <td width="20%"><input type="text" name="from" id="from" class="form-control" placeholder="From" value = '<?php echo $_GET["from"] ?? ''; ?>'></td>
    <td width="1%"></td>
    <td width="20%"><input type="text" name="to" id="to" class="form-control" placeholder="To" value = '<?php echo $_GET["to"] ?? ''; ?>'></td>
    <td width="1%"></td>
    <td width="10%"><input type="button" name="range" id="range" value="Search" class="btn btn-primary"><td>
  </tr>
</table><br>
<div id = "dashboard">

<script>
$(document).ready(function(){
  $.datepicker.setDefaults({
    dateFormat: 'yy-mm-dd'
  });
  $(function(){
    $("#from").datepicker().attr("autocomplete", "off");;
    $("#to").datepicker().attr("autocomplete", "off");;
  });
  $('#range').click(function(){
    var from = $('#from').val();
    var to = $('#to').val();
    var team = $('#team').val();
    if(from != '' && to != '' && team != '')
    {
      $.ajax({
        url:"range.php",
        method:"POST",
        data:{from:from, to:to, team:team},
        success:function(data)
        {
          $('#dashboard').html(data);
        }
      });
    }
    else
    {
      alert("Please select both team and date range");
    }
  });

  if($('#from').val() && $('#to').val() && $('#team').val()){

    $('#range').click();

  }

});
</script>

range.php (AJAX)

<?php

require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";

$email = $_SESSION['login_user'];


if(isset($_POST["from"], $_POST["to"], $_POST["team"]))
{

$result = '';
$query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id = '".$_POST['team']."' AND report_date BETWEEN '".$_POST["from"]."' AND '".$_POST["to"]."' ORDER BY ot_report.report_date DESC";
$sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sql -> execute();


if($sql->rowCount() > 0)
{
    echo'

    <form method="post" action="">
    <div class="row" style="height: 300px; overflow-y: scroll;">
    <div class="col-lg-12 grid-margin stretch-card">
    <table class = "table-bordered" width = "100%">
    <thead>
    <tr>
    <th>id</th>
    <th>Date</th>
    <th>Status</th>
    <th colspan = "2" width = "7%">Action</th>
    </tr>
    </thead>
    <tbody>';

    while($row = $sql->fetch(PDO::FETCH_ASSOC))
    {
        $datereport = $row['report_date'];
        $datereport2 = strtotime($datereport);
        $report_date = date('d M Y', $datereport2);

        $report_id = $row["report_id"];

        echo'<tr>';

            echo '<td>'.$report_id.'</td>';
            echo '<td>'.$report_date.'</td>';
            echo '<td align="center">';
            echo '<a class="btn-view btn-primary btn-sm" href="view_task/view_task.php?report_id='. $report_id .'" data-toggle="tooltip">View</a></td>';

            echo '<td align="center">';
            echo '<form action = "delete_ajax.php" method = "post" onSubmit=\"return confirm("Do you want to delete this report?")\">';
                echo '<input type = "hidden" name = "from" value = "'.$_POST["from"].'">';
                echo '<input type = "hidden" name = "to" value = "'.$_POST["to"].'">';
                echo '<input type = "hidden" name = "team" value = "'.$_POST["team"].'">';
                echo '<input type = "hidden" name = "report_id" value = "'.$report_id.'">';
                echo '<button type = "submit" class="btn-danger">Delete</button>';
            echo '</form>';

            echo '</td>';

        echo '</tr>';

    }

}

delete_ajax.php

  <?php

  require_once '../../../config/configPDO.php';

  $report_id = $_POST['report_id'];

  $sql = "DELETE FROM ot_report WHERE report_id=:report_id";
  $query = $conn->prepare($sql);
  $query->execute(array(':report_id' => $report_id));

  header("Location: dashboard_engineer.php?from='".$_POST["from"]."'&to='".$_POST["to"]."' &team='".$_POST["team"]."'");

  ?>

Can anyone knows what is the problem? The data cannot deleted!. Help anyone

1 Answers1

0

maybe this is because in the range.php file you have 2 form tags

first is outside while function

<form method="post" action="">

and second one is in while function

<form action = "delete_ajax.php" method = "post" onSubmit=\"return confirm("Do you want to delete this report?")\">

try removing the first one

  • It would probably be better to replace the first `
    ` tag that is outside the while function, and delete `
    `s inside the while function. You don't need all of those forms (one will do).
    – Jeff Vdovjak Feb 18 '20 at 06:59
  • yes that's right too, I'm just trying not to change the code too much above. but if you delete the form tags inside the while and change the form tags outside the while won't there be many fields in one form with the same name? @JeffVdovjak – aughyvikrii Feb 18 '20 at 07:08
  • Yes, you would have to alter the form a little. The submit button would actually have to carry the ID you wanted to delete. The *easiest* solution would be to do as you suggested. – Jeff Vdovjak Feb 18 '20 at 11:19
  • @aughyvikrii, hi bro.. can you solve this problem? https://stackoverflow.com/questions/60312073/php-mysql-error-after-delete-a-row-ta-php-page-filtered?noredirect=1#comment106687274_60312073 – Peter Sondak Feb 20 '20 at 04:00