[Ajax Fail] PHP Loading page way

C++, C#, Java, PHP, ect...
Post Reply
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

[Ajax Fail] PHP Loading page way

Post by overras »

Hello everybody. I'm working at my CMS platform, and recently I tried with a friend to implement pages with ajax and jquery. First thing was to do navigation from one page to another , with a nice effect. Anyway, let me explain what's the problem.

This platform, is based on index.php file, everything is there, including files, database connection and global variables like $x and also a file called template.inc.php where it loading the entire template and at content div, page asked in url.

Here is a screenshot :http://postimg.org/image/8knhwc1tj/

And here you can see how template "engine" is working: http://postimg.org/image/inngdfitv/

So, my url is: index.php?pag=FOLDER&sub=PHP_FILE&param1= etc

Let's take "AdminPanel" theme:

Code: Select all

<html>
<head>
          <title> Test Theme</title>
</head>
<body>
                 <?php
                    if(file_exists("app/Modules/$pag/$sub.php")) {
                        include("app/Modules/$pag/$sub.php");
                    }else{
                        include("app/Modules/dashboard/dashboard.php");
                    }
                 ?>
</body>
</html>
So, If I want php file "settings" from "account" folder (module) , I open this link: index.php?pag=account&sub=settings . Entire content of settings.php will be in AdminPanel theme because how you can see, I include it there. But, this theme is called by template.inc.php so, everything is from index.php , and when I try to do something in jquery, example, to load page instantly without refresh, I have to reload the entire page ( index.php?pag= etc) , so, is not an option . Is here a really problem? Is no way to do this If I load pages using $_GET ?

Is very important this organization because I'm going to create modules management and more, and what I want to know exactly, if this is a really wrong abordation for jquery/ajax , if there is no way to use jquery on this type of templating.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Ajax Fail] PHP Loading page way

Post by Jackolantern »

So just to be sure I get the problem, you are wanting to load in the result of a PHP script in only part of the page without doing a full refresh, right? Can you use jQuery.load(), or is there another problem with loading it in? I am not sure I understand where the problem is.

EDIT: I think I get a bit more of a problem, but can you explain exactly why you have to refresh the whole page instead of being able to use jQuery.load()? The load AJAX function will cause all your PHP to run on the back-end.

This really sounds like you are pushing what you can do without a proper MVC framework. I think if you were to even use a simple PHP framework like CodeIgniter it could really help out, since it would put a system into the core of your application that would allow you to piece together the HTML of your application.
The indelible lord of tl;dr
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: [Ajax Fail] PHP Loading page way

Post by overras »

Jackolantern wrote:So just to be sure I get the problem, you are wanting to load in the result of a PHP script in only part of the page without doing a full refresh, right? Can you use jQuery.load(), or is there another problem with loading it in? I am not sure I understand where the problem is.

EDIT: I think I get a bit more of a problem, but can you explain exactly why you have to refresh the whole page instead of being able to use jQuery.load()? The load AJAX function will cause all your PHP to run on the back-end.

This really sounds like you are pushing what you can do without a proper MVC framework. I think if you were to even use a simple PHP framework like CodeIgniter it could really help out, since it would put a system into the core of your application that would allow you to piece together the HTML of your application.
First , thanks for your answer Jack.

Second..well, If index loads te entire website, settings etc and THEME , so, when I try to load the page I must call index.php with these params: $pag where is the folder and $pag where is the php. So, if I try to load only #content div, content div will be equal with entire page. Like:

Code: Select all

// page wrapper
     //header menus etc
        // content
                    //======= after ajax loading
                            // page wrapper
                              //header etc
So, I must load the entire #pagewrapper to load that part because depdends on what is in the adress bar (index.php?pag=folder&sub=php_file_in_content_div) . And If I call the file directly like /path/file.php instead of that link , the pag will not work, because needs settings and more .
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Ajax Fail] PHP Loading page way

Post by Jackolantern »

You can make an AJAX Get request and send those parameters in the AJAX request. On the server-side, it will be indistinguishable from a direct page request. It will take a bit more work because you will have to create your own load() system to load in the results, but it definitely seems possible to me.
The indelible lord of tl;dr
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: [Ajax Fail] PHP Loading page way

Post by overras »

Hmm, so, is not impossible do to this on this type of navigation based on including files by url data, like $pag and $sub ?

If not, can you please share more details about this? How to do that request exactly? Is a normal request , idk, like:

Code: Select all

<script type="text/javascript">
$(document).ready(function() {
	$("#login").on("click", function(){
		var username = $("#username").val();
		var password = $("#password").val();
		
		$.post("index.php?mode=admin&sub=loginbox",{"username":username,"password":password},function(){
			
		});
	});
});
</script>
?
(anyway, dosen't work this too)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Ajax Fail] PHP Loading page way

Post by Jackolantern »

Check out this example on the jQuery website for making a get request:

Code: Select all

$.get("test.php", { name: "John", time: "2pm" } );
You include the variables in a separate object as the 2nd parameter. Now, combining that with the callback form to do something with the returned data:

Code: Select all

$.get("test.php", { name: "John", time: "2pm" }, function(data){
     //use jQuery to add what is returned into the part of the page where it is needed
});
Sorry if I am still missing what the main problem is.

It may be an issue where you have to shuffle around some of your PHP to make it work in this way.
The indelible lord of tl;dr
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: [Ajax Fail] PHP Loading page way

Post by overras »

I already tried this. Hmm, don't worry, let me explain again:

All what I try, to load with ajax/jquery any method, everytime I must load index.php, so everything, is like an refresh in ajax. I load settings, global variables, language, and, theme file, so I load the entire website and I don't know any way to load only #content because of this type of navigation based on url. I think I should rewrite it on MVC structure. :?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Ajax Fail] PHP Loading page way

Post by Jackolantern »

I don't want to say this is the only way to get this done. You can probably save the structure you have now by adding in some additional PHP scripts to glue together what you already have and more client-side scripts to change other parts of the page if you needed to. In the end, there is nothing you can't do with AJAX that you can do with standard HTTP requests (since AJAX requests are the exact same thing).

That being said, you will probably only get so far before hitting another issue like this. A good sign is that if you are using decision structures around PHP include() directives, you are probably stretching vanilla PHP a little too far, because include() was never designed to be used that way.

So I will say I think taking a slight detour and looking at what something like CodeIgniter or another MVC framework can offer you, you will end up ahead of the curve later because this type of functionality, at least to me, looks like it is simulating what MVC has out of the box.

If you do go MVC, if you need theming, CodeIgniter does not offer themes out of the box. If themes and skinning are important, Yii is an excellent framework that offers it out of the box. Fuel PHP is built out of CodeIgniter (the simplest of all PHP MVC frameworks to learn) but it has more features, such as theming, authorization, etc. Or you could choose my personal favorite PHP framework, Laravel. Both Laravel and Fuel were built out of my old favorite framework, CodeIgniter, but I like Laravel's documentation and style a bit better.
The indelible lord of tl;dr
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: [Ajax Fail] PHP Loading page way

Post by overras »

Thank you very much Jack, im going to rewrite everything on MVC structure, anyway, is time to learn something new, so, is a good conclusion about this current type of navigation. Thanks again, and, have a nice day!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Ajax Fail] PHP Loading page way

Post by Jackolantern »

Your most welcome :) I have a feeling you are going to love MVC once you get a handle on it :cool:
The indelible lord of tl;dr
Post Reply

Return to “Coding”