How is a PHP page updated/included via AJAX?

Asked

Viewed 51 times

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.phpdirectly 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);
}

1 answer

2


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

So PHP errors that you have are not related to Ajax or can be resolved by Ajax.

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

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

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

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

echo acoInclude('home.php');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.