Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry,

I have now read four of your books and have started the transition from procedural php to object oriented php. I use a lot of Javascript and Jquery Ajax requests now due to the enhanced benefits it can give you but am unsure of the best practice's of using ajax request with object orientated php programming, specifically structure! I wondered if you can maybe give me an example or point me in the right direction where i can learn this?

p.s.   I also wanted to say a massive thanks as I now work daily for a company programming!. This all started from the first book I read of yours "PHP and MySQL for Dynamic Websites Fourth Edition", I could not have got this far without you and your amazing books which I keep as treasures on my shelf and will do for the rest of my life! thanks for everything.

Look forward to your response,

Many thanks,

Sam 

Link to comment
Share on other sites

Hi, sam67. I'm not Larry, but if you don't mind, I'll chime in with my two cents:

For the most part, how you structure your PHP (procedural vs. OOP, etc.) doesn't really affect an Ajax request from JS and vice versa.

What I mean is, whether a request comes from a standard browser request or an Ajax request, PHP is more or less going to receive it the same way. The request is just a request, possibly with some data attached as a payload, and how you choose to handle that request in PHP is up to you.

At least, that's my immediate response to your question. I could be completely misunderstanding what you're asking though, so if maybe you could provide some more info and clarify where I'm maybe misunderstanding you, maybe I can help more.

Thank you.

Link to comment
Share on other sites

18 hours ago, HartleySan said:

Hi, sam67. I'm not Larry, but if you don't mind, I'll chime in with my two cents:

For the most part, how you structure your PHP (procedural vs. OOP, etc.) doesn't really affect an Ajax request from JS and vice versa.

What I mean is, whether a request comes from a standard browser request or an Ajax request, PHP is more or less going to receive it the same way. The request is just a request, possibly with some data attached as a payload, and how you choose to handle that request in PHP is up to you.

At least, that's my immediate response to your question. I could be completely misunderstanding what you're asking though, so if maybe you could provide some more info and clarify where I'm maybe misunderstanding you, maybe I can help more.

Thank you.

Hi HartleySan,

So if I take an example from the book, Chapter 9 (page 301), "The User Class". Lets say the data is being fetched by an ajax call rather then on page loads.

  1. You write your javascript part and post any data needed to the php ajax script. 
  2. then in your php ajax script would write your database query to fetch the user. 
  3. but would you try and add the result to a class defined as UserClass or send the result back via Json?

I am just a little unsure as to the cross between the two and maybe need to find an example to clear it up in my mind.

Many thanks for the time and help,

Sam 

Link to comment
Share on other sites

sam, there are a lot of ways you can do this, and there's no "right" answer.

I think #1 and #2 in your list are fine. Usually, in a modern MVC (model-view-controller) approach, you DB query for a user would take place in the user model, and your controller that receives the Ajax request would make the call to the user model to begin with.

More simply though, if you have a user class, in the script that receives the Ajax request, you can call methods in the user class however you see fit, and once you have all the data you need from the DB, yes, I would package it up into a logical format and send it back via JSON.

At that point, JS can do whatever it wants from the Ajax response to render the data accordingly.

Link to comment
Share on other sites

Hi HartleySan,

Thank you so much for the help, that makes perfect sense and I will try to write a test script that uses MVC, OOP and Ajax altogether with the approach you have given me!

thanks for clearing my mind up,

Sam.

   

Link to comment
Share on other sites

 Share

×
×
  • Create New...