I started thinking about using AJAX in an application I'm developing, but I've encountered some problems with it. I would like to understand how it works, to make the most of this "tool".
I had no problem adding static 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 is inside the file primfunctions.php
, within functions.php
which in turn is included in the index.
Basically: index.php <-> functions.php <-> primfunctions.php
When I run the script, a fatal error is returned to me
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 in pages.php
- for this I used the following logic: this (AJAX) should 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
So the questions remain: how are PHP scripts included via AJAX? Why is my script having these errors? How to solve them in a useful way for learning?
EDIT: Function acoInclude
function acoInclude($page){
include('/pages/'.$page);
}