javascript - Routing to Js file with CodeIgniter4 ← (PHP, CodeIgniter, JavaScript, JQuery, CSS, Bootstrap, HTML)

Let say i have this Public Folder inside the folder of iot-code-igniter\public\style

enter image description here

And i have file data.php inside folder iot-code-igniter\app\Views enter image description here

here the code that i tried to call the JS,CSS,and data.php and it's doesn't work, below is index.php :

<head>
    <script type = "text/javascript" src="../../public/style/js/jquery-3.4.0.min.js"></script>
    <script type = "text/javascript" src="../../public/style/js/mdb.min.js"></script>
    <script type = "text/javascript" src="../../public/jquery-latest.js"></script>
    <script type = "text/javascript">
        var refreshid = setInterval(function(){
            $('#grafik').load('/data.php');
        }, 1000);
    </script>

</head>
<body>
    <div class="container" style="text-align: center; margin-top: 24px;">
        <h3>Grafik Sensor</h3>
        <p>(Result data)</p>
    </div>

    <!-- tempat grafik -->
    <div class="container">
        <div class="container" id="grafik" style="width:80%; text-align:center;"></div>
    </div>

</body>
</html>

Question :

  1. How i can call the src file inside the head?
  2. How i can load the data.php to render/extend it?

Answer



Solution:

Use this code

<head>
  <script type = "text/javascript" src="../public/style/js/jquery-3.4.0.min.js"></script>
    <script type = "text/javascript" src="../public/style/js/mdb.min.js"></script>
    <script type = "text/javascript" src="../public/jquery-latest.js"></script>

</head>

Answer



Solution:

The public folder is the root folder for the web browser, and your paths need to be absolute or relative to that folder.

You need to remove any prefix to public.

<script type = "text/javascript" src="/style/js/jquery-3.4.0.min.js"></script>

Answer



Solution:

In the folder public create assets/css/... and assets/js/...

<link 
  href="<?php echo base_url('assets/css/material-icons.css'); ?>" 
  rel="stylesheet" 
  type="text/css"
>

<script src="<?php echo base_url('assets/js/bootstrap.min.js'); ?>"></script>

Source