How is a page updated in PHP via AJAX?

1

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);
}
    
asked by anonymous 30.04.2017 / 09:56

1 answer

2

PHP and Ajax script are not related. You can think of Ajax as a "visit" to the browser, which calls a PHP file and returns the response it gives you, as if it were a user.

So the PHP errors you have are not Ajax-related and can not be solved by it.

What to do then?
The errors you have happen because you are calling a function that is not defined in PHP. To define it you must include the file (s) that contain this function.

So you have to put it in your PHP file, before this switch :

require_once('primfunctions.php'); // corrige o caminho se não for este

so the error should disappear. After that I assume that this acoInclude('home.php'); function returns HTML, in which case you should have echo before, so it actually shows the result to whoever is looking for it. A browser or Ajax:

echo acoInclude('home.php');
    
30.04.2017 / 10:03