在PHP Echo中调用Javascript函数 [英] Call Javascript Function in PHP Echo

查看:450
本文介绍了在PHP Echo中调用Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此链接 ,我正在尝试从表中动态删除行.这是我的Javascript函数:

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 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 successfully
                if(result=='success'){
                     //Check random no, for animated type of effect
                     var 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中的 Echo 调用它似乎并没有调用它.这是我的PHP代码:

However, calling it from Echo in Php, doesn't seem to invoke it. Here's my PHP code:

echo "<td align=\"center\">" . $id."</td>";
echo "<td><a href = 'javascript:deleteBox($id)'>Delete</a></td>";

无论我哪里出错,都请纠正我.尽早提供帮助将是非常感谢.

Please correct me wherever I'm goin wrong. An early help would be highly appreciated.

推荐答案

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

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

在我看来,这就是我的方式.

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

编辑并缩短了jscript;

Edited and shortened the jscript;

function deleteBox(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 successfully
                if(result=='success'){
                     //Check random no, for animated type of effect
                     var 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);
                }
          }
  });  
} 

在您的delete.php中:

in your delete.php:

$_POST['id'] 

检索ID.

这篇关于在PHP Echo中调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆