Skip to content Skip to sidebar Skip to footer

Call Javascript Function In Php Echo

With reference to This link , I am trying to delete rows dynamically from a table. Here's my Javascript function: function deleteBox(id){ alert ('Inside Method'); if (confirm('Are

Solution 1:

<td><ahref = 'javascript:deleteBox($id)'>Delete</a></td>

to

echo "<td><aonClick='deleteBox(" . $id . ");'>Delete</a></td>"; 

In my opinion, thats how I would do it..

Edited and shortened the jscript;

functiondeleteBox(idDelete){

alert ("Inside Method");
if (confirm("Are you sure you want to delete this record?"))
{
  $("#flash_" + idDelete).show();
  $("#flash_" + idDelete).fadeIn(400).html('<img src="img/loading.gif" /> ');

  $.post('delete.php', {'id': idDelete}, function(result) {
            if(result){
                $("#flash_" + idDelete).hide();
                // if data delete successfullyif(result=='success'){
                     //Check random no, for animated type of effectvar randNum=Math.floor((Math.random()*100)+1);
                     if(randNum % 2==0){
                        // Delete with slide up effect
                        $("#list_" + idDelete).slideUp(1000);
                     }else{
                        // Just hide data
                        $("#list_" + idDelete).hide(500);
                     }

                }else{
                     var errorMessage=result.substring(position+2);
                     alert(errorMessage);
                }
          }
  });  
} 

in your delete.php:

$_POST['id'] 

to retrieve the ID.

Solution 2:

Check this, Hope this helps. Instead of id, static values are given

<tdalign="center">1</td><td><ahref='#'onclick='deleteBox(1)'>Delete</a></td>

echo "<td><ahref='#'onclick='deleteBox(1)'>Delete</a></td>";

jsfiddle

I am updating the answer, check whether alert is working.

<script>functiondeleteBox(a){
    alert(a);
}   
</script><?phpecho"<a href='#' onclick='deleteBox(1)'>Delete</a>";
?>

Solution 3:

Since you're using jQuery, I wouldn't do the call to the function in the href. Try something like this:

Javascript:

$(function() {
$('.delete').click(function() {

var id = $(this).attr('data-id');

alert ("Inside Method");
if (confirm("Are you sure you want to delete this record?"))
{
  var dataString = 'id='+ id;
  $("#flash_"+id).show();
  $("#flash_"+id).fadeIn(400).html('<img src="img/loading.gif" /> ');
  $.ajax({
  type: "POST",
  url: "delete.php",
  data: dataString,
  cache: false,
  success: function(result){
           if(result){
                $("#flash_"+id).hide();
                // if data delete successfullyif(result=='success'){
                     //Check random no, for animated type of effectvar randNum=Math.floor((Math.random()*100)+1);
                     if(randNum % 2==0){
                        // Delete with slide up effect
                        $("#list_"+id).slideUp(1000);
                     }else{
                        // Just hide data
                        $("#list_"+id).hide(500);
                     }

                }else{
                     var errorMessage=result.substring(position+2);
                     alert(errorMessage);
                }
          }
  }
  });
});
});

PHP/HTML:

echo "<tdalign=\"center\">" . $id."</td>";
echo "<td><aclass='delete'data-id='" . $id . "'>Delete</a></td>";

Post a Comment for "Call Javascript Function In Php Echo"