【问题标题】:ajax code contains "PHP site_url" with parameters in ajaxajax 代码包含带有 ajax 参数的“PHP site_url”
【发布时间】:2021-05-20 23:30:02
【问题描述】:

我想问,如果ajax代码包含“PHP site_url”和ajax中的参数

,有没有可能

这是具体代码:

"<a class='btn btn-info' href="<?php 'echo site_url("data_detail/detail_datas/vieworc/ " + item.NIK + "); '?>">Detail</a>"

这是完整的代码:

                 .done(function(dt){
                        if(dt != null){
                            console.log(dt)
                            table.rows().remove().draw();
                            $.each(dt, function(i, item){
                                table.row.add([
                                    item.Name,
                                    item.NIK,
                                    item.gender,
                                    item.PositionDesc,
                                    item.Shift,
                                    item.tgl,
                                    item.Attendance,
                                   "<a class='btn btn-info' href="<?php 'echo site_url("data_detail/detail_datas/vieworc/ " + item.NIK + "); '?>">Detail</a>"
                                ]).draw();
                            })
                        }
                    })
                });

【问题讨论】:

  • 不,尝试将逻辑传递给前端
  • 您需要从 PHP 代码中删除 item.NIK 部分。
  • @Phil:是的,但是,我需要在 site_url 中包含“ITEM.NIK”才能根据 NIK 移动页面
  • @lay :是的,但是,我需要在 site_url 中包含“ITEM.NIK”才能根据 NIK 移动页面

标签: javascript php ajax codeigniter


【解决方案1】:

请试试这个

"<button type='button' class='btn btn-success' onclick='showDetail(\""+ item.NIK + "\");' >Detail</button>"

【讨论】:

    【解决方案2】:

    如果你的 site_url() 只连接字符串,你可以这样做:

    // Pass the result as a js variable
    const siteUrlTpl = "<?php echo site_url('data_detail/detail_datas/vieworc/{NIK}'); ?>";
    // ...
    .done(function(dt){
        if(dt != null){
            console.log(dt)
            table.rows().remove().draw();
            $.each(dt, function(i, item){
                table.row.add([
                    item.Name,
                    item.NIK,
                    item.gender,
                    item.PositionDesc,
                    item.Shift,
                    item.tgl,
                    item.Attendance,
                    // Replace the {NIK} to the real value
                    "<a class='btn btn-info' href='" + siteUrlTpl.replace('{NIK}', item.NIK) + "'>Detail</a>"
                ]).draw();
            })
        }
    })
    

    【讨论】:

    • 感谢您的帮助,但我发现了错误,错误:3:419 Uncaught ReferenceError: siteUrlTpl is not defined at Object. (3:419) at Function.each (jquery.min .js:2) at Object. (3:409) at u (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at k (jquery.min .js:2) 在 XMLHttpRequest. (jquery.min.js:2)
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    相关资源
    最近更新 更多