1
I started thinking about using AJAX in an application I’m developing, but I found some problems with it. I would like to understand how it works, to make the most of this "tool".
I had no problems adding static pages or simple php scripts.
I have, until then, in my mind that the function sends the request to the server, stores the response in a variable and we decide what to do with it.
$('li > a').click(function(evt){
evt.preventDefault();
var href = $(this).attr('href');
var pages = "includes/pages.php?page=" + href;
$.ajax(pages, {
beforeSend: function(){
$('#content').fadeOut();
},
success: function(response){
$('#content').html(response).fadeIn();
}
});
My biggest question about adding/updating files to the page via AJAX is about includes and requires.
<?php
$page = @$_GET['page'];
switch ($page) {
case 'home':
acoInclude('home.php');
break;
case 'ceps':
acoInclude('ceps.php');
break;
case 'clientes':
acoInclude('clients.php');
break;
default:
acoInclude('home.php');
break;
}
?>
The php file in question has a custom function (acoInclude()
) that’s inside the archive primfunctions.php
, inside functions.php
which in turn is included in the index.
Basically: index.php <-> functions.php <-> primfunctions.php
When running the script, I get a fatal error
Fatal error: Call to undefined function acoInclude() in C:\wamp64\www\gn\sistema\includes\pages.php on line 7
But if I include the file primfunctions.php
directly on pages.php
- for this I used the following logic: this (AJAX) must work like an iframe, and one script must be independent of another - I get another fatal error
Fatal error: Cannot redeclare acoInclude() (previously declared in C:\wamp64\www\gn\sistema\includes\primfunctions.php:16) in C:\wamp64\www\gn\sistema\includes\primfunctions.php on line 18
Then there are the questions: how are PHP scripts included via AJAX? Why is my script with these errors? How to solve them in a way that is useful for learning?
EDIT: Function acoInclude
function acoInclude($page){
include('/pages/'.$page);
}