Hello,
I try to ajaxify my project and this works perfectly.
The ajax onclick event works.
But when I visit the url through my browser (e. g. visit my homepage via external website)
of course no content will be displayed, because it is not an ajax request.
I tried a couple of things but I can not manage it.
File: application/core/My_Controller
PHP Code:
class MY_Controller extends CI_Controller
{
protected $templatedata = array();
public function __construct()
{
parent::__construct();
//collect all css, js stuff
//$this->templatedata['css'] = all css stuff and so on
}
public function __destruct()
{
parent::__destruct();
echo $this->load->view('deskapp2/html/index',$this->templatedata,true);
}
}
So first I collect my css and js files.
In the destructor I echo-out my main-template.
File: application/view/deskapp2/html/index
Code:
<html>
<head>
<!-- loop css files -->
</head>
<body>
<!-- part of the menu -->
<li><a class="loveit" rel="index" href="<?php echo $base;?>welcome">Test 1</a></li>
<li><a class="loveit" rel="mytest" href="<?php echo $base;?>welcome/mytest">Test 2</a></li>
<!-- this is for the ajax-request -->
<div id="showmystuff"></div>
<!-- this is for the non-ajax-request -->
<?php echo $content;?>
<!-- other js files -->
<script type="text/javascript">
$(document).ready(function(){
$('.loveit').click(function (e) {
var targetUrl = $(this).attr('href');
var targetUri = $(this).attr('rel');
var arrUrl = targetUri.split('/');
//alert(arrUrl);
//url:"http://localhost/3113/public/ajax/index"
e.preventDefault();
window.history.pushState("","",targetUrl);
$.ajax({
url:"http://localhost/3113/public/ajax/index",
type: "POST",
data:{arr:targetUri},
dataType:"html",
success:function(data){
//alert('test');
$('#showmystuff').html('<b>'+data+'</b>');
},
error:function (){
alert('Something went wrong');
},
});
});
});
</script>
</body>
</html>
File: controller/Ajax.php
This controller is for ajax calls.
PHP Code:
class Ajax extends CommonController {
public function __construct()
{
parent::__construct();
}
public function index()
{
if($this->input->is_ajax_request())
{
echo 'You are here: '.$this->input->post('arr'); //shows the full uri-string of the link which is clicked, see javascript in index.html
}
}
public function __destruct()
{
parent::__destruct();
}
}
My plan here is of course to load all the stuff from a database depending on the uri.
File: controller/welcome.php
PHP Code:
class Welcome extends FrontendController {
public function __construct()
{
parent::__construct();
//some stuff
}
public function mytest()
{
if($this->input->is_ajax_request())
{
echo $this->load->view('welcome_message', NULL, true);
}
else
{
$this->templatedata['content'] = 'some stuff';
}
}
public function index()
{
if( !$this->input->is_ajax_request())
{
$this->templatedata['content'] = 'load from db...';
}
}
public function __destruct()
{
parent::__destruct();
}
}
I know that the Ajax-Controller is not really nessecary.

I just play around.
Summary:
How can I differentiate between an ajax-request and a non-ajax-request?
Any hints?
Thanks to everyone who reads this.
And like I said. I am not professional and I just play around to improve myself.