1

i have retrieved a record from mysql table using php. Each record is retrieved with a form and a delete button. the user is supposed to click the delete button and then the record on that row is deleted. Problem is i want to display an alert before deletion, how do i do this since the record to be deleted has an ID stored in a hidden field inside each form. my retrieved table below;

echo'<tr><td>'.$sn.'</td>
  <td>'. htmlspecialchars($r['Year']).'</td>
  <td><form method="post" action="awardyear.php">
     <input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
  </form>
  </td>
  <td><form method="post" id="awardForm" action="awardyear.php">
      <input type="hidden" name="yearId" value="'.$r['YearID'].'"/> 
      <input type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
      </form></td>
      </tr>';

Any help will be appreciated, thanks.

chidiebere
  • 13
  • 1
  • 3

4 Answers4

0

Change the form to a link and use JS to redirect to the delete page on confirm.

HTML:

<a href='#' onclick='areYouSure("awardyear.php?deleteYear=Delete&amp;yearId=$YearID")'>DELETE</a>

Javascript function:

function areYouSure(location) {
    if(confirm("Are you sure you want to delete this entry?")) {
        document.location.href = location;
        return true;
    } else return false;
}
MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
0

Try ..

echo'<tr><td>'.$sn.'</td>
  <td>'. htmlspecialchars($r['Year']).'</td>
  <td><form method="post" action="awardyear.php">
     <input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
  </form>
  </td>
  <td><form method="post" id="awardForm" action="awardyear.php">
      <input type="hidden" name="yearId" value="'.$r['YearID'].'"/> 
      <input onclick="return myConfirm();" type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
      </form></td>
      </tr>';

// Java Script for this

function myConfirm() {
  var result = confirm("Want to delete?");
  if (result) {
   return true;
  } else {
   return false;
  }
}
Vinay
  • 324
  • 1
  • 13
0

Try this

HTML

echo'<tr><td>'.$sn.'</td>
<td>'. htmlspecialchars($r['Year']).'</td>
<td><form method="post" action="awardyear.php">
 <input type="hidden" name="yearId" value="'.$r['YearID'].'"/>
</form>
</td>
<td><form onsubmit="return deleteThis();" method="post" id="awardForm" action="awardyear.php">
  <input type="hidden" name="yearId" value="'.$r['YearID'].'"/> 
  <input type="submit" name="deleteYear" class="btn btn-danger" value="Delete">
  </form></td>
  </tr>';

JAVASCRIPT

function deleteThis(){
   if(confirm("Are you sure you want to delete?")){
        return true;
   }else{
        return false;
   }
}
Malik Naik
  • 1,472
  • 14
  • 16
0

Use this to alert before delete

$('#awardForm').find('.btn-danger').on('click',function(){
    var hiddenValue = $("input[name='yearId']").val();
    alert('You sure want to delete '+hiddenValue+' ?'); 
});
rocky
  • 631
  • 5
  • 14