View Single Post
Old 04-25-2008, 08:36 PM   #1 (permalink)
EyeDentify
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default Prototype and URL encodeded strings

Sorry if this is in the wrong place but i think the root of the problem lies in the JS part.

I will try to explain in the easiest way my problem.

First, i´m using the latest Prototype JS framework (1.6.0.2).
I´m having no problem getting it to work except for the darn URL encode bit.

And just to avoid any confusion about 'Kat' in the code.
I´m using that word because in swedish 'Cathegorys' would be 'Kateogorier'.

Now my very simple JS code to accomplish a AJAX GET call is:

Code:
function MyLinkKat(MyUrl, MyKat)
{

var MyUrl;
var MyKat; //This is were my cathegory comes

	new Ajax.Request(MyUrl, {
	  contentType: 'application/x-www-form-urlencoded',
	  encoding: 'UTF-8',
	  method: 'get',
	  parameters: '?kat=' + MyKat + '',
	  onCreate: function(transport)
	  { 
	  	var MyStatusText = $('MyStatusDiv');
	  	new Ajax.Updater(MyStatusText, 'ajax_loader.php');
	  },
	  onSuccess: function(transport)
	  { 
	  	var MyResponseText = $('MyResponseDiv');
	  	MyResponseText.update(transport.responseText);
	  },
	  onComplete: function(transport)
	  { 
	  	var MyStatusText = $('MyStatusDiv');
	  	MyStatusText.update('');
	  }
	});
}
I´m thinking something goes wrong at the 'parameters' option in the Ajax.Request.
What do you think ?

I´m calling this code like this:

Code:
<A HREF="javascript:MyLinkKat('ajax_links.php', 'Bloggs & Misc')">Bloggs & Misc</A>
Now my problem seems to be that this string 'Bloggs & Misc' don´t seem to be URL encoded when transported throug the JavaScript and when it reaches the 'ajax.php' script nothing matches cause i only get 'Bloggs' left.

Now i thought to myself, this is easy to fix. So i tryed this aproach:
Code:
<A HREF="javascript:MyLinkKat('ajax_links.php', '<?PHP echo(urlencode("Bloggs & Misc")); ?>')">Bloggs & Misc</A>
But that didn´t work either.

For the sake of argument let´s say that the following code is in the receiving script:
PHP Code:
<?PHP
$sql 
"SELECT * from blogg_links WHERE kategori='" utf8_decode($_GET['kat']) . "'";
?>
Should i try using urlencode() inside the utf8_decode() function on the $_GET['kat'] variable ?

Do you have any suggestions or ideas to what i could change to make this work, or do you want any other information to help me resolve this mather don´t hesitate to ask.

It might be a simple solution that i haven't thought about in my frustration.

Hope you get what i´m getting at.

It´s kinda late and think i´m gonna give this a break for tonight.

Cheers :)

-----------------------------------------
Update:

Well i had some sleep and came up with this temporary solution that works for me. Im using the following PHP functions that i came up with to do some "Ajax Encoding" and "Ajax Decoding" as i like to call it. Wich means i simply replace some chars like 'spaces' and '&' signs with other safe chars that wont 'get lost' in the transport. i made a Encode and Decode function.

Here it is:
PHP Code:
<?PHP
function ajaxEncode($string)
{
    
$target_array[] = ' ';
    
$target_array[] = '&';

    
$replace_array[] = '_';
    
$replace_array[] = 'och';

    
$string str_replace($target_array$replace_array$string);
    return 
$string;
}
function 
ajaxDecode($string)
{
    
$target_array[] = '_';
    
$target_array[] = 'och';

    
$replace_array[] = ' ';
    
$replace_array[] = '&';

    
$string str_replace($target_array$replace_array$string);
    return 
$string;
}
?>
But i would still like to get some explaination if there is one to why my orignial problem existed if someone has an answer?

Cheers again :)
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.

Last edited by EyeDentify : 04-26-2008 at 08:04 AM. Reason: Temporary Solution
EyeDentify is offline  
Reply With Quote