CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a JavaScript (http://www.codingforums.com/forumdisplay.php?f=19)
-   -   $_GET 100% in Javascript (http://www.codingforums.com/showthread.php?t=183556)

barkermn01 12-02-2009 04:06 PM

$_GET 100% in Javascript
 
Hey all i have spent about 2 hours today getting this working cross browser and working properly.

This is a 100% Implementation of php $_GET and works perfectly in JavaScript

And when i say 100% i mean it, none of this key=val it works with array[]=>val

E.G if you have the url hello.html?page=2&filters[]=minPrice&filters[]=maxPrice&minPrice=100&maxPrice=1000

and as you can see there is an array inside this GET information filters[]
it will setup $_GET['filters'][0] = minPrice and $_GET['filters'][1] = maxPrice

So you want to see the code here it is::
PHP Code:

var $_GET = {};
var 
undefined;
function 
readURL() {
  var 
tLoc ""tPairs "";
  var 
tGet = [];
  var 
arrays = [];
  var 
foundGet = -1;
  
tLoc window.location "";
  
foundGet tLoc.indexOf('?');
  if (
foundGet > -1) {
    
tLoc tLoc.substring(foundGet 1tLoc.length);
    
tPairs tLoc.split('&');
    for (var 
0tPairs.lengthi++) {
      
tGet tPairs[i].split('=');
      
foundArray tGet[0].indexOf('[');
      if(
foundArray > -1){
          
foundArrayEnd tGet[0].indexOf(']')
        if(
foundArrayEnd > -1){
          
old tGet[0];
          if (
arrays[tGet[0]] === undefined){
            
arrays[tGet[0]] = 0;
            
$_GET[tGet[0].replace("[]","")] = {};
          }else{
            
arrays[tGet[0].replace("[]","")] = arrays[tGet[0]] + 1;
          }
          
$_GET[tGet[0].replace("[]","")][arrays[tGet[0]]] = decodeURIComponent(tGet[1].replace(/+/g,' '));
        }
      }else
          
$_GET[tGet[0]] = decodeURIComponent(tGet[1].replace(/+/g,' '));
      }
    }
  }
readURL(); 


Kor 12-02-2009 05:32 PM

What's the use? As long as you don't use a Data Base I might rise a legitimate question: why to submit data? To where? To whom?

If you need to send data to DB, you need a server-side solution. If you don't ... well, if you don't, why bother to submit data?

Do I miss something?

barkermn01 12-04-2009 04:00 PM

Quote:

Originally Posted by Kor (Post 894742)
What's the use? As long as you don't use a Data Base I might rise a legitimate question: why to submit data? To where? To whom?

If you need to send data to DB, you need a server-side solution. If you don't ... well, if you don't, why bother to submit data?

Do I miss something?

This works with URLS on a massive scale i have built it as i have a search that allows users to enable filters and disable filters now i was not doing URL rebuilding a redirecting for every filter they applied,

So JavaScript can $_GET so it can loop though them add them to a new URL string and allows you to filter the $_GET to how you want it, once the site that uses it is live i will show you an example

My Data is used in both php and JavaScript form the URL so i need both but i have an Array in the GET so i needed to build a parser for it


All times are GMT +1. The time now is 09:45 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.