The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 06:16:13 PM

Login with username, password and session length


Pages: 1 [2]
  Print  
Author Topic: My first AJAX project, biting off way more than i can probably chew  (Read 8081 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #15 on: July 29, 2008, 01:14:56 PM »

i know.

question..

So you basically use JSON for encoding an array on the server side, so they can then be decoded on the client side, and they are still an array correct?
Or short version, JSON is to encode an OBJECT for transit?

never used JSON so, just making sure. Read a few pages about it and thats what it seems like. Though you can obviously use it to encode functions as well it seems.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #16 on: July 29, 2008, 01:43:35 PM »

json is actually a protocol rather than a function set for JS or PHP.

In php it's json_encode and json_decode. It serializes either an object or an array into a string, almost exactly like serialize, except that it is json notation.

In javascript, by saying var arr = eval('(' + aJSONStr + ')'); you are unserializing, that's it. The JSON notation can be used literally as well quite effectively - I've often put big ol' strings onto a variable in JS and then simply eval'd them to liven an array.

The problem is that there is no json_encode in Javascript. So I use a function called stringifyer that is a modified cersion of one I found a while ago. I'll post it in the JS repository in a moment. Here's the handiness: if you have a whole lot of complex data to get back up to the server, make it an array in JS, encode it and send as one big blob rather than trying to emulate this with lots of postParams. Excellent for multiply nested arrays as well. This has completely replaced XML for me because I can send hierarchical data back and forth without any need for parsing.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #17 on: July 30, 2008, 06:57:45 PM »

ok i get it.

So next question.

cookies, how do you deal with them?

server side doesnt seem to work, which i didnt think it would, so you have to do them client side right? In particular, for cookies that a planted AFTER the page is finished loading, and would be created from an AJAX call.

is that right?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #18 on: July 30, 2008, 07:18:27 PM »

bah, don't do that. it'll make your page that much easier to spam with a robot!
Logged

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #19 on: July 30, 2008, 08:21:25 PM »

i have little concern of spamming in this app. It is a zero yield target, so far. There is 1 vector i have thought of, but I have that covered i think (think myspace worm type of situation).

at first i was surprised that you cant set a cookie from the server via the ajax call, but then realized that it is not a complete communcation, the same way an http page load is. So, i guess i have to do client side cookie setting.

I have NEVER done client side cookie handling, so this should be a new experience...
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #20 on: July 30, 2008, 10:25:33 PM »

Cookies are a breeze. One of the earlier posts I made here was my personal local storage handler, here it is in the JS repository:

http://www.perkiset.org/forum/javascript_ajax/efficient_local_client_storage_of_cookies-t86.0.html

Quiet easy in fact. Create an instance of the class, set a "storage file name" and set/get name/value pairs. Easy peezie, japaneesie (Channelling my gramps for a moment there). You can only store about 20 cookies normally per domain, but with this class I compress them into a single string so you can use the full 4096 of a single cookie "slot" for any number of cookies. The "storage file name" is the one-of-20 that you are allowed to use per domain.
« Last Edit: July 30, 2008, 10:27:07 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #21 on: July 30, 2008, 10:31:51 PM »

actually i figured it out anyway. didnt know about the 20 cookie limit, but no biggy, i am only doing 2 and everything else will be serverside. but i will check out your class because maybe then I will just jam stuff into the cookie instead.

ok i have to share.
This little methodology is working pretty well for me so far. I am sure I will paint myself into a corner at some point, but so far so good
Obviously I am leaving out a bunch of stuff, but the response is JSON.
Code:
function handleAjax(sender)
{
var arr = eval('(' + sender.lastResponse + ')');
for (var i in arr['display'])
{
document.getElementById(arr['display'][i]['parentid']).innerHTML = arr['display'][i]['html'];
}
for (var i in arr['cookie'])
{
Set_Cookie( arr['cookie'][i]['name'], arr['cookie'][i]['value'], arr['cookie'][i]['days']);
}
}
its pretty cool. I can send back an arbitrary number of things, and they all loop and get processed.

idunno i just had to share.

I now have a login,registration,forgot password popup box done. all ajax. yeay me.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #22 on: July 30, 2008, 10:45:27 PM »

NBs, FTW!

Looks like it's just about all under the hood now mang. Go man go!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
svakanda
Expert
****
Offline Offline

Posts: 131



View Profile
« Reply #23 on: January 29, 2009, 05:46:16 AM »

ugh...i've been meaning to start playing around with AJAX, and that was a most reconvincing discussion.
  instead i think i'll go boil my brains in bunny blood.  pheww...
Logged

a ship is safe in the harbor, but that's not what it's for.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #24 on: January 29, 2009, 07:44:11 AM »

It's better than it sounds svak Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #25 on: January 29, 2009, 11:22:56 AM »

It's really not that bad. You don't have to write all of *that much* javascript.

I tend to keep my javascript confined completely to the facet of communication, and Perk's ajaxrequestor class is more than adequate for that. I don't really have much logic in the js. That's probably because I hate the language so very much. I tend to not touch much javascript at all, and I still use ajax when it's appropriate or would enhance the app (keywords there, don't use it if there's not a need, you're just asking for headaches).
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #26 on: January 29, 2009, 01:59:10 PM »

To VS' point, you can really get away with almost no JS, if your important for you to do so. Personally, I've worked really hard to get JS under my belt, along with some rather advanced CSS, PHP and MySQL stored procedures so that I can really spread the processing out. But you can absolutely build an AJAXy site with minimal JS and almost 100% PHP.

The technique is to use my ajaxRequestor, then simply put <whatever is returned from the server> into the innerHTML of a div. Looks sort of like this:

req = new ajaxRequestor();
req.url = '/handler.php';
req.postParam('requestType', 'getSomeHTML');
req.onSuccess = function(sender) { document.getElementById('resultsDiv').innerHTML = sender.lastResponse; }
req.execute();

in this little chunk of code we instantiate an instance of the ajax requestor, tell it what script on the server is to be called (handler.php), post a parameter, "requestType" - this is totally arbitrary, but I like vars that mean something. handler.php will use this value from the $_POST array. Then tell the requestor that, upon successful return of something, to put it as is into the HTML div with an id of "resultsDiv." Finally we pull the trigger with the execute method.

All processing, formatting etc is then done on the PHP side, rather than the local side. This method looks a lot like iFrames because it very simply brings HTML (or whatever) back from the server and puts it into a div. But it'd definitely get you started.

Good luck Svak.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
svakanda
Expert
****
Offline Offline

Posts: 131



View Profile
« Reply #27 on: January 29, 2009, 05:32:21 PM »

that seems doable.  I'll definitely play around with it after all.  Ty both for the encouragement!


Logged

a ship is safe in the harbor, but that's not what it's for.
carlosnava312
n00b
*
Offline Offline

Posts: 1


View Profile
« Reply #28 on: October 24, 2011, 10:01:14 AM »

what kind of game is that once ? Can you please elaborate little more about it ? I will be waiting for your description about that game ! Is there something else that maybe I will help you !
Logged

Pages: 1 [2]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!