The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Integrate Slim into already working AJAX Livesearch

J.'s Avatar

J.

07 Aug, 2012 10:04 AM

Hey folks,

I am just starting out with REST APIs and need some advice.

Over the last couple of weeks I coded an AJAX Livesearch in form of a dictionary. The data is stored in several tables within a MSSQL Database. The user has the possibility to search GER --> ENG, ENG --> GER and to suggest new translations, etc.

Now all this is working just fine and I thought about expanding this project. I want to be able to search for translations via URL, so that means using a REST API if I understood everything correctly.
My problem is that I don't have a clue on how to include Slim into my livesearch.... I tried the hello World Tutorial and included an extra <?php ?> section in my index.php, however, after loading my localhost (using XAMPP for local testing) it just showed some messed up version of my original homepage with the livesearch (the formatting was all messed up) and in the "background" there was an 404 message, supposedly from Slim.

Here's a general overview of my index.php:

It starts with a <?php ?> section, which inhabits a function for the submission of new translations.
Followed by declaring the head of the html and some other general stuff.
Followed by a huge
Finally a huge section of HTML codes for all the elements of the homepage and especially the livesearch searchbox and result window(s).

In addition to the index.php there are also about 10 other .php files for the database search, etc.

I hope you get the picture, otherwise just tell me and I try to get some code snippets in here.
Would really appreciate if some of you pro's could help me including Slim into all this and give me a general direction of where to implement the new stuff from it. Basically all I want is to be able to retrieve translations via URL, e.g. typing http://localhost/index.php?key="hallo" or something should give me the translation of "hallo", which hello.

best regards,
J.

  1. 1 Posted by J. on 07 Aug, 2012 10:09 AM

    J.'s Avatar

    Somehow it cut out the 3rd point. It's supposed to be "Followed by a huge section of javascript"

  2. Support Staff 2 Posted by Brian Nesbitt on 07 Aug, 2012 01:09 PM

    Brian Nesbitt's Avatar

    Don't have time for a longer answer... if you can post your whole index.php we can help your reorganize it using Slim routing and templates to help separate your html / javascript and code.

  3. 3 Posted by J. on 07 Aug, 2012 01:39 PM

    J.'s Avatar

    2nd try. Somehow my original answer didnt get posted... Attached the index.php, however, I shortened it considerably. Main functionality still there though.

  4. Support Staff 4 Posted by Brian Nesbitt on 08 Aug, 2012 02:45 AM

    Brian Nesbitt's Avatar

    It looks like the index.php is incomplete as it ends with an opening td then closes body and html tags... obviously not right.

    As for just moving the current stuff to Slim you need to move the functionality into your routes and the html/javascript into a template. It looks like you have the same page receiving the initial page load and then also receiving later form submissions, hence $_POST['Submission'] == "Submit". With a route based framework you typically break up the requests over multiple routes, perform logic and then pass the results on to the template/view to render the calculated results. Generally this seems like a single page application. So I would use a single GET route to serve up the initial html/javascript. Then use jquery/ajax to send the search/submission requests to a few POST routes (as many as needed, maybe one per type of search??) to respond with html rendered search results and inject those into your current dom.

    I think I would need to see more of the application to help more. You can start a github repo or something to show more if needed.

    Some side notes:

    You should look at a javascript library like jQuery. Your js wouldn't be so verbose or so browser independent as the library takes care of that for you.

    There seems to be a lot of repetitive code with very small differences that should be refactored into a few common simple functions.

    It doesn't look like you are sanitizing/escaping your variables being set in the INSERT statement.... ie. open to simple SQL injection attack.

    Its also typically frowned upon to post your system administrator password publicly :-(

  5. 5 Posted by J. on 08 Aug, 2012 07:35 AM

    J.'s Avatar

    Thanks for the feedback.
    The incompletion was probably caused by me deleting like 2000 lines of extra code in this index.php. But yeah im pretty new to web programming and stuff so I guess those repititions come as a side effect with this. Will try to do kind of a code cleanup to get rid of some of those dirty passages.
    Will definitely have a look at jQuery. As for the SQL "problem", it doesn't really matter in this stage, as this application is just used locally or in an closed local network. Nobody from "outside" would even have access to it. And luckily for me the password is just a throwaway I thought of just for this testing purposes. Thanks for the hint though ;-) Will be off to read about REST routing now, thanks for the help.

  6. Support Staff 6 Posted by Andrew Smith on 08 Aug, 2012 09:33 AM

    Andrew Smith's Avatar

    I deleted your index.php file that was attached for security reasons as stated by Brian, its not good to post your system administrator password publicly. I hope all the information Brian provided has been useful to you, I will be closing this ticket since it seem to be resolved.

  7. Andrew Smith closed this discussion on 08 Aug, 2012 09:33 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac