To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Misc Help > ClientSide Technologies

ClientSide Technologies Discuss HTML/CSS/Javascript, and any other client-side technologies, here.

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 4.00 average. Display Modes
Old 02-24-2006, 02:09 PM   #1
mikesch
Member
 
Join Date: Jan 2006
Posts: 32
[RESOLVED] urlencode in JavaScript?

Hi,

I have a php page where I output a list of titles and give the option to delete them. When the user clicks on delete, I call the following JavaScript function:

function deleteitem(title) {
if (confirm("Are you sure you want to delete '" + title +"'"))
{
window.location.href = 'outputadmin.php?del=' + title;
}
}

If the user confirms, then the title is deleted from the database. Everything works as long as I don't have a title that contains the '&' (ampersand) character. For example, if I have the following title: 'John & Mary', then after the user confirms his wish to delete, the following url will be sent: outputadmin.php?del=John&Mary

The problem is that when I access $_GET[del] I will get $_GET=John and that's not the title in the database. I know 'Mary' is treated as separate variable just like 'del'.

Is there a way around this in JavaScript to urlencode the whole title? In other words, to skip the '&'?

Thank you very much.

Best,

Mike
mikesch is offline   Reply With Quote
Old 02-24-2006, 03:12 PM   #2
mikesch
Member
 
Join Date: Jan 2006
Posts: 32
Talking

Found the answer. FYI: In Javascript, the function escape() does the same as urlencode() in PHP.
mikesch is offline   Reply With Quote
Old 03-20-2007, 10:05 AM   #3
Ichiro
Junior Member
 
Join Date: Mar 2007
Location: Visiting the Wizard
Posts: 19
Not the same

Actually Javascript's escape() and unescape() are not the same as PHP's urlencode() and urldecode(). In fact, there is one particular occasion when assuming they are the same can cause serious trouble.

You can see a table of the results of the two different methods at http://cass-hacks.com/articles/discu...encode_decode/

I know this was posted a long time ago but I just got here.
Ichiro is offline   Reply With Quote
Old 10-23-2007, 06:29 AM   #4
stijnm
live from my basement
 
Join Date: Oct 2007
Location: Belgium
Posts: 23
Just got here too.

How about this:

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}
stijnm is offline   Reply With Quote
Old 09-29-2008, 07:58 PM   #5
nubs
Member
 
Join Date: Apr 2005
Posts: 41
Quote:
Originally Posted by stijnm View Post
Just got here too.

How about this:

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}
This is shorter:

function urlencode(str) {
return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

function urldecode(str) {
return unescape(str.replace('+', ' '));
}
nubs is offline   Reply With Quote
Old 11-02-2008, 04:00 PM   #6
ottodv
Junior Member
 
Join Date: Nov 2008
Posts: 2
The solutions above only replaces the first occurrence of each pattern. It's better to use a global replace:

function urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}
ottodv is offline   Reply With Quote
Old 11-07-2008, 10:16 PM   #7
ottodv
Junior Member
 
Join Date: Nov 2008
Posts: 2
In addition to my previous post I think it would be good to point out that there is also the encodeURIComponent() function in JavaScript that is probably suitable for the purposes intended in this thread. It doesn't escape: ! ~ * ' ( ) like the urlencode PHP function does, but I have experienced no problems passing a argument encoded by encodeURIComponent to a PHP script. Another plus is that encodeURIComponent encodes all UTF-8 characters while escape encodes ISO Latin characters.
ottodv is offline   Reply With Quote
Old 06-25-2009, 04:33 AM   #8
ProteusR3
Junior Member
 
Join Date: Jun 2009
Posts: 1
Thumbs up

I've got another solution.
I've tested it and it works very well!
function php_urlencode (str) {
str = escape(str);
return str.replace(/[*+\/@]|%20/g,
function (s) {
switch (s) {
case "*": s = "%2A"; break;
case "+": s = "%2B"; break;
case "/": s = "%2F"; break;
case "@": s = "%40"; break;
case "%20": s = "+"; break;
}
return s;
}
);
}
ProteusR3 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:51 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.