13 Replies - 9190 Views - Last Post: 13 April 2011 - 09:11 AM Rate Topic: -----

#1 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Ajax/JS with PHP MVC?

Posted 05 April 2011 - 05:20 AM

Hi,

I'm currently trying & planning to make an app using the ajax technique, however i could use
some tips or hints on how to deal with Ajax in an PHP MVC application. I'm using only one
entry file say Index.php with controllers, views and models.

From most if not all the examples and "tests" that I've done/come across using Ajax they always seem to implement it all on the same page, call me a noob but i just have a hard time figuring it out how i could apply ajax on PHP mvc with only one entry file.

I'd be glad for any tips, hints, tutorials... or if its even possible at all?

Background to the project is that I wanted to make a more fluid application instead of having
constant postbacks so maybe a message board , shoutbox etc but done using ajax/js.

- Cheers, any help to bump me in the right direction is appreciated

This post has been edited by heady89: 05 April 2011 - 05:22 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Ajax/JS with PHP MVC?

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Ajax/JS with PHP MVC?

Posted 05 April 2011 - 05:29 AM

What AJAX does is in simple terms communicate with the server asynchronously. You PHP script will then get the data via a POST or GET method. AJAX will get any data that has been outputted via the script...eg. echo, print, printf, var_dump...etc.

//AJAX
...
xmlhttp.onreadystatechange = function() {
   if(xmlhttp.readyState == 4)
        alert(xmlhttp.responseText); //this should alert Data received if it was successfull.
}
xmlhttp.open('GET', 'script.php?foo=bar', true);
xmlhttp.send();


//PHP
<?php $foo = $_GET['foo'];
echo "Data received"; ?>


AJAX can come in handy in instances where you just need to POST data to a PHP script without having to do unnecessary reloading.

What is it exactly puzzles you about AJAX to be a bit more specific?

This post has been edited by codeprada: 05 April 2011 - 05:32 AM

Was This Post Helpful? 2
  • +
  • -

#3 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Ajax/JS with PHP MVC?

Posted 06 April 2011 - 03:00 AM

Thanks for the thoughts.

So would it be a reasonable way to have say an Ajax.php file which i contact from my JS and inside i would have different if(issets) and functions etc depending on the parameters that are passed to the file?

I suppose thats how to do it, since if i would just return the entire response text and have the actual code in any of my regular controllers(example LoginController) etc i would just return a duplicated of the entire page.

Index.php
<script type="text/javascript" src="Javascript/Ajax/ajax_framework.js" language="Javascript"></script>




AjaxController.php
<?php

	if(isset($_GET['foo']))
	{
		$foo = $_GET['foo'];
		echo "Data received";
	}


Ajax/JS code
function test()
{
	http.onreadystatechange = function()
	{
		if(http.readyState == 4)
		document.getElementById("errordiv").innerHTML = http.responseText;
	}
	http.open('GET', 'Controller/AjaxController.php?foo=bar', true);
	http.send();

}
function createObject()
{
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();


This post has been edited by heady89: 06 April 2011 - 03:04 AM

Was This Post Helpful? 1
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Ajax/JS with PHP MVC?

Posted 06 April 2011 - 05:08 AM

Yep that's how it's done.
Was This Post Helpful? 0
  • +
  • -

#5 justforgetme  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 05-September 09

Re: Ajax/JS with PHP MVC?

Posted 06 April 2011 - 12:14 PM

A quick note, whenever I have used AJAX in an MVC model I actually treated it a a view, since in most cases you will be needing to create a big part of the model of the application anyway it really isn't that big of a deal.

One thing you should decide early on is what formate you want to use for the responses. Many times it is ok to just request html as a response but when you are working with big or more complex datasets you usually will want ther response text to be xml or json (which I use most of the time). The reasoning behind this is that xml and json are structured data that can be manipulated after arrival whereas html you will usually just want to append or replace with parts of the already existing web page.
Between xml and json I prefer json because of the small data overhead, on all other points both formats are pretty much the same except if you want to send object methods in your response in which case json is the only way to do it conveniently.

just my 2 cents though.
Was This Post Helpful? 1
  • +
  • -

#6 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Ajax/JS with PHP MVC?

Posted 10 April 2011 - 03:09 PM

Ok,

I've run into an small issue when using Ajax with forms.

Since the server is talking with another file behind the scenes(Ajax.php) my action='".htmlentities($_SERVER['REQUEST_URI'])."' part of the form tag refers to the Ajax.php file when trying to submit a form instead of how it was previously where it refers to the current location of the user.

This causes some issues since, it will go to the ajax.php page when hiting the submit button instead of staying.
If i make any sense at all.. is there any convenient way to do this ?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Ajax/JS with PHP MVC?

Posted 10 April 2011 - 04:01 PM

I think you're talking about a form submitting after the ajax call. You want to do something like:
onclick = "yourajaxcall(); return false;"

This post has been edited by CTphpnwb: 10 April 2011 - 04:02 PM

Was This Post Helpful? 1
  • +
  • -

#8 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: Ajax/JS with PHP MVC?

Posted 10 April 2011 - 04:41 PM

View PostCTphpnwb, on 10 April 2011 - 04:01 PM, said:

I think you're talking about a form submitting after the ajax call. You want to do something like:
onclick = "yourajaxcall(); return false;"


Thanks, btw would i be wrong in treating the ajax file as a controller?
I saw one of you apparently treat it as a view.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Ajax/JS with PHP MVC?

Posted 10 April 2011 - 05:22 PM

If you want to be strictly MVC, then the Javascript would send a request calling on a php file that was the controller and the controller would call/include the model code where needed and finally call/include the view. This of course is all outside the scope of Javascript, which simply makes the request to the server and then accepts a respones. ;)

This post has been edited by CTphpnwb: 10 April 2011 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 justforgetme  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 05-September 09

Re: Ajax/JS with PHP MVC?

Posted 12 April 2011 - 03:51 PM

View Postheady89, on 10 April 2011 - 05:41 PM, said:

Thanks, btw would i be wrong in treating the ajax file as a controller?
I saw one of you apparently treat it as a view.


EDIT:

View PostCTphpnwb, on 10 April 2011 - 06:22 PM, said:

If you want to be strictly MVC, then the Javascript would send a request cal...

@CTphpnwb:
javascript != ajax
/EDIT

I think you are misunderstanding what I said or I misunderstood your point in the first post.

As you say you use one entry point index.php so this is the controler of you application (actually its this and the files it calls that have to do with controling the apps behavior.) the controler get the request and decides how to build the model wich then is returned to the user via a view. So what you get as
request.responseText 
is actually the view of the application.

to give you an example:
an non-MVC app would get accessed this way
http://example.com/?page=h431fD45
and return something, most probably a valid html page

the point with MVC is that when you have created an application you can easily change the way it presents the data by means of writing another view.

so our hypothetical site would be able to be accessed like this:

http://example.com/?page=h431fD45&view=html

which obviously returns an html page (that is what real links would look like)

http://example.com/?page=h431fD45&view=pdf

which returns a pdf formated document (mine types and all included, this when clicked by a visitor would actually start a download)

http://example.com/?page=h431fD45&view=ajax

which invokes the ajax view of the model and returns ajax

so snippet no 1 would be used to display a page. (it would be valid html, include doctypes headres etc.)
snippet no 2 would be used by a user to download for example a story of the site. (it would be a valid pdf file)
and snippet no 3 would be used by an XMLhttp call. This most probably would be raw data, maybe XML if you like the overhead, possibly JSON or even html markup (if you are lazy and just want go and modify teh html view).

now as you see there is no need for an ajax.php entry point to your application. In fact I think it goes quite against MVC theory (or was it KISS?) in that you have many controllers for the application whereas you should, actually, have many of all the other stuff (models and views). Ok, wikipedia may say that there can be many controllers on a single implementation but this would be helpful if you were making a standalone java application, where every component of a UI can be controlled by it's own controler, but not so on the Internet (at least for medium complexity implementations) because most of the time as your project grows you will end up generating more and more of the same model for every request.

as said before these are my personal spins on the subject and if someone has a different opinion they should share it.

check this out MVC [wikipedia]

well, it's late as it was the last time i posted here so...

This post has been edited by justforgetme: 12 April 2011 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: Ajax/JS with PHP MVC?

Posted 12 April 2011 - 06:08 PM

I think you're misunderstanding what ajax is. As seen in Wikipedia, it does require the use of Javascript.

My point was that when the Javascript requests information from the server it doesn't care how the server provides that information. It doesn't even care if the server uses PHP! If certainly doesn't know or care whether or not a PHP script uses MVC!
Was This Post Helpful? 0
  • +
  • -

#12 justforgetme  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 05-September 09

Re: Ajax/JS with PHP MVC?

Posted 13 April 2011 - 12:31 AM

I get what you mean but the thread clearly asks how to treat ajax requests server side.

so we are not talking about what ajax is but what ajax requests are to the server ;-)

And I agree the javascript won't care if the server runs php java or compiled c++

This post has been edited by justforgetme: 13 April 2011 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#13 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: Ajax/JS with PHP MVC?

Posted 13 April 2011 - 04:14 AM

Maybe a quick note to this thread. I would suggest using a framework when using javascript and ajax nowadays (I prefer jQuery). If you use a framework you don't have to think about browser compatibility (IE is a bitch).
Was This Post Helpful? 0
  • +
  • -

#14 justforgetme  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 05-September 09

Re: Ajax/JS with PHP MVC?

Posted 13 April 2011 - 09:11 AM

View Postnightscream, on 13 April 2011 - 05:14 AM, said:

M(IE is a bitch).


Don't tell me... I've just spent half a day (yes that's 12 hours) debugging IE javascript and css... Well, ok mostly css but I also fell on a nasty compilation error that killed my mind for some hours
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1