Search code examples
phpajaxcurlcodeigniter-3href

adding a href in ajax while using codeigniter and getting data from curl


I want to add a link inside AJAX and needs a html.tpl[i]['nip'] in a tag because I want to display another view that required data with the chosen nip.

This is for PHP language in framework CodeIgniter and getting data from CURL. I have tried different ways to solve it but still, display wrong when I add link function, the AJAX is not working (not display the data).

I expect the output is when the link of detail is click the will display the view of data required with the nip. I expect the output is the table working properly and can choosing the link and can display the view that the data is required with the nip.

What's going wrong?

+"<td><a href='<?php echo site_url('admin/detail/');?>' >detail</a></td>"
document.addEventListener("DOMContentLoaded", () => {
  $.ajax({
    url: "<?php echo site_url('Admin/piljur');?>",
    dataType: "json",
    type: "POST",
    cache: false,
    success: function(html) {
      var data = "";
      for (var i = 0; i < html.tpl.length; i++) {
        data += "<tr><td>" + (i + 1) + "</td>" + "<td>" + html.tpl[i]['nip'] + "</td>" + "<td>" + "<td><a href='<?php echo site_url('admin/detail/'//i want to adding the value (html.tpl[i]['nip']);?>' >detail</a></td>" + "</tr>";
      }
      $("#datatabel").append(data);
    }
  })

Solution

  • You could separate the detail url into a variable to be used later after the ajax call success, like this :

    document.addEventListener("DOMContentLoaded", () => {
      let detail_url = '<?php echo site_url('admin/detail/'); ?>';
      $.ajax({
        url: "<?php echo site_url('Admin/piljur');?>",
        dataType: "json",
        type: "POST",
        cache: false,
        success: function(html) {
          var data = "";
          for (var i = 0; i < html.tpl.length; i++) {
            data += "<tr><td>" + (i + 1) + "</td>" + 
                    "<td>" + html.tpl[i]['nip'] + "</td>" + 
                    "<td><a href='" + detail_url + html.tpl[i]['nip'] + "' >detail</a></td></tr>";
          }
          $("#datatabel").append(data);
        }
      });
    }