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
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 '&'?
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.
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.
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;
}
);
}