Thread: Novul
View Single Post
  #60  
Old 07-12-2015, 07:20 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 92,439
Kyttias is on a distinguished road
Default

Here to day with a 'minor' yet groundbreaking update for me. I officially figured out why AJAX calls from Javascript don't get along with the Mysidia framework. Because PHP and Javascript are two very different languages, communication between them must be precise. The PHP file an AJAX call is directed to must only render the exact data you're looking for - usually with echo - and it can be HTML or an object (or array) rendered as plain text, which will be converted into a format Javascript can use. It must not render anything else!

In the Mysidia framework, everything is routed through the index using mod_rewrite in the .htaccess file - for good reason, of course, we don't want people accessing files they aren't supposed to! But this meant that whenever I made an AJAX call to a PHP file, the HTML of the entire index page, that is, the entire page template, also rendered out, not just what I had echoed! My solution was this: Create a new folder inside the Mysidia directory, and remove the mod_rewrite rule from it with a new .htaccess file stored inside it. I named this folder "ajax" and I'll store all my AJAX-related PHP files inside of it. The .htaccess file I made inside it only has one line:

Code:
RewriteEngine Off
Problem solved.

Okay, so, why AJAX? Why go through all that trouble? Well... magic. Magic is why.

With AJAX, you can avoid the additional page load caused by regular form submission. This is more significant than it sounds. As a prototype example, below is what is known as a "live" search - you don't have to press Enter or click any search button - and results will automatically be recommended to you. (To spare the server database some load, the query is only sent off through AJAX after there's more than 3 characters in the search box AND 500ms have passed since the last key pressed came up.) This is using real data from my 'adopts_items' table:


Alright, probably impractical to search for items this way, right? But what if you forgot a user's name and wanted to send off a PM? Just start typing, and AJAX will find you close matches. Why bother reloading the page with a form submission just to check if a username is typed correctly? Or worse, typing the whole message only to get an error that the user you're trying to send it to doesn't even exist because you typed it in wrong? And now your entire message is gone! Snap. AJAX to the rescue, see? That'd be a better example, but I just don't have users in my database to show that off effectively, lol~

But, speaking of searching for items, I decided to go ahead and rewrite the existing item search that came with Mysidia with an AJAX solution. In addition to removing the need for a page reload that a traditional form submission would require, I also made it so an empty search still returned results (sorted by 'category'). For an extra dose of improvement, I implemented a Javascript plugin called "Stupid Table" that allows results to be sorted client-side, both alphabetically (item name) and numerically (price).

First, I'll show the default item search on a fresh install. Keep watching and I'll reveal mine:


There are some differences between mine and the original item search. I don't include options to search by store or to limit the price, for example. (But this comes down to personal preference and how I want my site.) I also have a column in my database for an item's "rarity" - which is shown by the color of the text used for the item's name. Rarity will usually denote the strength of an item, how much it costs, or how difficult it is to obtain.

Anyway, that's all for now!

If anyone has AJAX-related questions I can probably answer them now, aha... I'm willing to share the files for the first example I gave if you want to have a look, too.

On a final note, since my Novu are so custom, I've had to create my own pet search for them, as well:
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.

Last edited by Kyttias; 07-13-2015 at 12:29 AM.
Reply With Quote