• 0

Ajax w/ jQuery in external js file...and one other thing


Question

So, I've been doing more and more web-development.  I have fairly strong PHP, HTML, ASP, jQuery, etc experience.. but I'm not sure the best way to manage something.  Now, I come from a very object oriented way of programming and the use of multi-use functions so maybe this is what is messing me up.

So.. say I have an ajax function that runs a get on a php page, and I return an array of results from the database in json format.  In some cases I may want that array dumped into a table.. but in other cases I may want it in a drop down list.

Now.. I know I could do it a couple of ways.. 

1) Have the function to fill the table of data in one page, then have the same function except filling a drop down in another page.   I don't like this because I have the same AJAX code in two spots, the only difference being the result of the success.

2) Have a function that runs the ajax which then takes a callback to a function which is run on success.  The problem with this is that some callbacks may have more parameters than other callbacks so not sure how to make that clean per-se.

3) Have a function that runs the ajax and takes a bunch of parameters then based on those parameters do different things in the success/fail.  

4) Not have all sorts of parameters, and make the ajax return the data on success.. but would have to not make it async or my data would come back after my calling function has been executed.

Is there a better way of doing it, or a preferred way?

Also, in ASP I can do like sections where certain data appears on the page based on the content ID.  How can I do something like that in PHP.  Right now I have all my scripts being loaded in the head.. but would like them at the bottom of the body.. but I would need them BEFORE any per-page inline scripts.  Without having an include in each php page telling it to include the scripts.. or putting them in script to be run.. how would I do that?

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.