Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to include PHP scripts again in a ajax loaded PHP page?

Tags:

jquery

php

I have an ajax call to a php page in which I pass an ID using GET.

<script>
$("#oid").change(function(){
    var oid = $(this).val();
    alert(oid);
    $.ajax({
        type: "GET", 
        url: "tabela_reservas.php",
        data: "oid="+oid,
        success: function(html) {
            $("#tabela_reservas").html(html);
        }
    });
});
</script>

It works, except that PHP gives me an error:

Fatal error: Class 'Reserva' not found in C:\xampp\htdocs\kwagenda\tabela_reservas.php on line 20

If I include my class files in the "tabela_reservas.php" it works. But these Class files are already loaded/included in my "index.php" from where I'm calling this ajax page.

My question is: do I need to include my php Class file on "tabela_reservas.php" again or there is another way of doing it?

I ask this because, to me, it doesn't seem to be a very elegant solution, and it look's like an overhead, since I'll be loading twice the same thing on my page.

Is this the right thing to do or there is another way of doing this?

Thanks!

like image 818
resmall Avatar asked Jan 03 '13 12:01

resmall


People also ask

How Ajax get data from another page in PHP?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="" src="action. js"></script> <? php $id = $_GET['id']; echo $id; ?>

Why we are Using AJAX in PHP?

AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.


2 Answers

When you make an ajax request to the server, this is a completely new request, just like navigating to another page.

So yes, you need to include everything again.

You should look into autoloading of classes for a more elegant solution to include classes.

like image 182
jeroen Avatar answered Sep 20 '22 23:09

jeroen


You need to include all class files again in the tabela_reservas.php as there is no relation between index.php and tabela_reservas.php.

All ajax requests are completely new without any relation to the current(caller) page. Consider those as a new page being called :)

like image 32
Arvind Bhardwaj Avatar answered Sep 18 '22 23:09

Arvind Bhardwaj