Devo Avidianto P
Devo Avidianto P

Reputation: 75

How To Auto Load div content in PHP codeigniter?

I have view \views\ngoding\AutoLoad.php :

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
setInterval(function(){
$("#content").load('data.php')
}, 5000);
});
</script>

<style>
#content{
background-color: #00A1E0;
font-size: 24px;
font-weight: bold;
padding-top : 10px;
color : #fff;
min-height: 200px;
}
#content,h1 {
    text-align: center;
}
</style>

<title>Auto Load Page in Div using Jquery</title>
</head>
<body>
    <h1>Auto Load Page in Div</h1>
    <div id="content">Please wait....</div>
</body>
</html>

I have code after load 5 second views\ngoding\load.php :

<?php 

$err = file_get_contents("application\logs\log.php");
echo preg_replace(array('/(^|\R)ERROR\s*-\s*/', '/(^|\R)(.*?)\s*-->\s*/'), array('$1', '$1$2 '), $err);
echo "<br>";
echo "Content akan load selama 5 detik";

for ($i = 0; $i <=10; $i++) {
    echo $i. "<br/>";
}

?>

I have Controller controllers\AutoLoadDiv.php : Controller

I have log file Like this logs\log.php :

ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502

I have code like this to auto load div 5 second , I want to auto load content views\ngoding\load.php

why my code to auto load div cannot run , can you resolve my code?

Upvotes: 1

Views: 1096

Answers (1)

Touheed Khan
Touheed Khan

Reputation: 2151

I think there is some errors in your current code. I suggest you to follow below code to achieve Auto reload div.

  1. I have changed $("#content").load('data.php') to $("#content").load('AutoLoadDiv/getData') in view.
  2. I also added getData() in your controller to fetch the log.(your load.php code is now in this function).

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class AutoLoadDiv extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('ngoding/AutoLoad');  
    }

    public function getData() {
        $err = file_get_contents("application\logs\log.php");
        echo preg_replace(array('/(^|\R)ERROR\s*-\s*/', '/(^|\R)(.*?)\s*-->\s*/'), array('$1', '$1$2 '), $err);
        echo "<br>";
        echo "Content akan load selama 5 detik";

        for ($i = 0; $i <=10; $i++) {
            echo $i. "<br/>";
        }
    }

}

View : ngoding/AutoLoad

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
    setInterval(function(){
    $("#content").load('AutoLoadDiv/getData')
    }, 5000);
    });
</script>

<style>
#content{
    background-color: #00A1E0;
    font-size: 24px;
    font-weight: bold;
    padding-top : 10px;
    color : #fff;
    min-height: 200px;
    }
    #content,h1 {
        text-align: center;
    }
</style>

<title>Auto Load Page in Div using Jquery</title>
</head>
<body>
    <h1>Auto Load Page in Div</h1>
    <div id="content">Please wait....</div>
</body>
</html>

Upvotes: 1

Related Questions