One PHP page that handles many functions: AJAX, PHP, jQuery-VBForums
Results 1 to 5 of 5

Thread: One PHP page that handles many functions: AJAX, PHP, jQuery

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    757

    One PHP page that handles many functions: AJAX, PHP, jQuery

    Been a long time since I've posted...

    I plan on redoing a CMS I built using PHP/MySQL.

    This time around I want to employ jQuery and AJAX.

    Just a simple question to start....

    It was once recommeded to me that I reference a separate php file that contains all of my php custom functions. I use a lot of the same functions in different pages. It makes sense.

    When submitting a php form, I plan on having referencing a javascript file that contains code that loops through all the form elements and formats the entered values as JSON.

    The php form will look something like this:

    Code:
    <form action = "ajax/contact-form.php" method = "post" class = "ajax">
    form input fields and submit button here
    </form>
    Any form in my CMS that has class 'ajax' will be looped and formatted as JSON via my ajax file.

    My javascript page then calls the jQuery post method and sumbits the data to mySql db. Kinda like this:

    Code:
    $('form.ajax').on('submit', function() {
    
    var that = $(this),
     url = that.attr('action'),
    type = that.attr('method'),
    data = {};
    
    ...
    ...
    
    $.ajax({
    url: 
    type: post
    data: JSON data from above JS;
    
    }
    
    });
    The js uses the form action to determine what ajax php page to process. That php page (ajax/contact-form.php) has the command to insert into my db.

    The snippet of code above is NOT MINE. I have been studying ajax and jquery. It is example code.

    I understand how it works I feel, but how can I have multiple insert functions all in ONE php page? Let's say I have another form on a separate page...

    I'd want the php method to reference the same ajax php page, but I'd want it to use a different INSERT statement to save it properly in that page. I'm guessing that's easiest... that way I don't have tons of separate php files that are running my insert statements per the form that's submitted.

    I really hope I made some sense here. If not, I'll try to explain more...

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,886

    Re: One PHP page that handles many functions: AJAX, PHP, jQuery

    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    757

    Re: One PHP page that handles many functions: AJAX, PHP, jQuery

    Thank you for taking the time to respond. Since I've posted, I found some great YouTube tutorials on the subject.

    All of them reference an outside javascript file that uses ajax to process a given form. Each ajax method calls a separate outside php file that handles the processing for that specific form (has the Insert query).

    I guess I'd have to create a separate php file for each form to handle the insert query. I was wondering if that is how it was normally done, or if one could reference a single php file for all the forms and then call the appropriate insert query from within that php file to handle the form specific insert.

    I am guessing that's not really the best approach. I'll just have to create the separate php files. Any comments?

    On another note, I was wondering what the point would be to use AJAX when inserting my data for my CMS? I can see an advantage for using AJAX to pull mySql data: no page refresh is cool to display data. But, if inserting a new contact, for instance, in my contacts table, it seems natural to relocate the user to the new contact page information after inserting new contact data. I am thinking using an AJAX script isn't a necessity in this case. A new page would have to be loaded anyway. Might as well pass the new contact form data to itself to process the insert and then relocate the user. Any comments on that?

    An AJAX insert would be a nice touch if the user is filling out a "Contact Us" form... when the user types in a message and after it's submitted, a nice message appears that says "Thanks for contacting us... your message was successfully sent!"

    Thanks again.

    I'm trying to think this through before I redo my CMS design.

  4. #4
    PowerPoster
    Join Date
    May 2002
    Posts
    25,269

    Re: One PHP page that handles many functions: AJAX, PHP, jQuery

    A new page doesn't have to be loaded (in the case of a new contact) ... the AJAX would be responsible for gathering the data from the form, sending it to the server and then immediately updating the screen to say "Contact added!" or "Thanks for the feedback... it's like hte QuickReply section of this page... the whole page isn't reloaded (well, except for those times when the site is a bit wonky) it sends the info to the server, gets a response back (presumably the new HTML from the new post and any others that happened in the mean time, and then it uses that HTML segment to update the existing display.

    Now, yes, if you want to reland the user to the page displaying the contact info, then a more "standard" post would probably suffice... you could also still send the data via AJAX, and then when you get the response form the call, then redirect the user to the new contact's page... there's a number of ways to slice and dice it.


    Now... to the original question ... yes, it's possible, if you have the right framework to work with... I use CodeIgniter http://ellislab.com/codeigniter in the past... it's a good MVC framework to work with. Classes are defined by the file, then the methods are defined within that class... and then you can call them byt specifying a particular queryustring, or if you have rewrite rules on, by using a "folder" structure.... for instance:
    www.mysiteexample.com/Contact/AddNew ... that would invoke the AddNew method of the Contact class.... www.mysiteexample.com/Contact/View/133 ... would invoke the View method of the Contact class and pass in 133 as the ContactID (assuming that it's the only parameter expected) Each of those methods, AddNew, View, delete, etc are all in the same PHP file... using that same idea, the link for this thread would be www.vbforums.com/Thread/View/7373983

    So it might be something to look at.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    757

    Re: One PHP page that handles many functions: AJAX, PHP, jQuery

    Thanks, techgnome. Since your post, I've been trying to learn OOP! I think that's my best option.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.