dongzhi7763 2016-09-09 05:57
浏览 40
已采纳

在动态按钮上调用参数化函数在php中单击

I want to call a parameterised function in PHP. I am using AJAX for that.

This is my dynamic button. I need to pass $sub_id to the delet(sub_id) function and perform there some SQL.

echo "<td><input type='submit' name='disable' value='Disable' onClick='delet($sub_id);'/></td>"; 

This is my script so far:

<script>
function delet(sub_id) {
    alert("sub ID = " + sub_id);
    jQuery.ajax({
        type: 'GET',
        url: '/my_page',
        data: {
            ID: sub_id
        }
    }).done(function(msg) {
        alert("Data Saved: " + msg);
    });
}
</script>

and this is PHP function:

function delet($ID){
    echo $ID;
    //do sql stuff...
}

All the code is in one file and I am doing this in wordpress default template swentysixteen. so there is no .php extension to my page.

when I click on the dynamic button, I am getting 404 error in url parameter of ajax. When I see console, the url link seems to be correct.

Please help me in this issue. Thanks.

  • 写回答

1条回答 默认 最新

  • drutjkpsr67393592 2016-09-09 06:13
    关注

    First of all, dont combine in your mind JS code and PHP code into one.

    Please set in your mind that JS code is executes on Browser which is provided by server through PHP. PHP code executes on Server side and provide HTML code as output to client(browser). so JS variables and PHP variable have no any relationship directly or indirectly.

    For your solution,

    You have passed ID parameterusing GET by ajax. so in SErver side you will get passed data in $_GET variable so ID will get by $_GET['ID']

    function delete($ID){
       echo $ID;
       //do sql stuff...
    }
    if(isset($_GET['action']) && $_GET['action']=="delete")
    {
        if(isset($_GET['ID']) && !empty($_GET['ID']))
        {
           delete($_GET['ID']);
           echo "deleted successfully";
        }
        else
        {
           echo "ID not passed for delete record";
        }
        die(); // you stop execution of PHP after delete opration here
    }
    

    Javascript Code

    <script>
    function delet(sub_id) {
        jQuery.ajax({
            type: 'GET',
            url: '/my_page',
            data: {
                action: 'delete',
                ID: sub_id
            }
        }).done(function(msg) {
            alert("Data Saved: " + msg);
        });
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大