ajax code contains "PHP site_url" with parameters in ajax

I want to ask, is it possible, if the ajax code contains "PHP site_url" with parameters in ajax

this is the specific code:

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

this is the full code:

                 .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();
                            })
                        }
                    })
                });

1 thought on “ajax code contains "PHP site_url" with parameters in ajax”

  1. If your site_url() only concats strings, you can do something like this:

    // 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();
            })
        }
    })
    
    Reply

Leave a Comment