Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2004, 12:55 AM   PM User | #1
egquen2000
New Coder

 
Join Date: Feb 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
egquen2000 is an unknown quantity at this point
Unescape Not Working

Hi,

I've put up a form on my website that is supposed to pass the entered values from one page to a new page that opens and show the data exactly as typed on the new page. I'm using a javascript that I got from another javascript site that i have modified to fit my needs. For some reason, any spaces that are input into the form are still being displayed on the next page as "+" signs. For example, if I input "New York," it will show up on the new page as "New+York." How do I fix this problem? There is already coding in place that uses the unescape function, but it still won't display properly. Could someone please have a look at my webpage to see if they can figure out the problem? It's at melaniestype.com/serviceagreement.html. The output form is at melaniestype.com/serviceagreementoutput.html. I'd appreciate a quick response. Thanks.
egquen2000 is offline   Reply With Quote
Old 02-02-2004, 01:28 AM   PM User | #2
egquen2000
New Coder

 
Join Date: Feb 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
egquen2000 is an unknown quantity at this point
HERE'S PART OF THE CODE

<!-- Begin
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
// End -->
</script>
</HEAD>

<!-- STEP THREE: Put this on the page that should read the values -->

<BODY link=forestgreen vlink=forestgreen><nobanner>



<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
requiredclientname = unescape(params["requiredclientname"]);
requiredclientID = unescape(params["requiredclientID"]);



When I say

document.write("Client name is " + requiredclientname + ".");

for example, it prints wrong.
egquen2000 is offline   Reply With Quote
Old 02-02-2004, 02:40 AM   PM User | #3
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Here's a quick fix:

Code:
requiredclientname = unescape(params["requiredclientname"]).replace(/\+/g, " ");
.....Willy
Willy Duitt is offline   Reply With Quote
Old 02-02-2004, 02:45 AM   PM User | #4
egquen2000
New Coder

 
Join Date: Feb 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
egquen2000 is an unknown quantity at this point
Oh, thank you so much! Finally works.


But how does that code actually work? What's with the front and back slashes? Sorry if I sound like, duh, cause I'm new to this whole thing.
egquen2000 is offline   Reply With Quote
Old 02-02-2004, 02:47 AM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You should unescape the name also:

params[unescape(nameVal[0].replace(/\+/g, " "))]=unescape(nameVal[1].replace(/\+/g, " "));

See this thread for the issues in parsing querystrings.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 02-02-2004, 03:07 AM   PM User | #6
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
It's a regular expression and since I am still struggling with them,
I am not qualified to offer a substantive explanation. But, I'll try.

The forward slashes / are the wrapper.
The backslash \ escapes the reserved +
And the g indicates it is to be global. (replace all instances of)

Liorean has a good tutorial on regular expressions
but I do not have the link handy.

Furthermore, Glenns solution is much better
Not only will the pairs name be covered, you won't
need to add the .replace(...) to all of your values.

.....Willy
Willy Duitt is offline   Reply With Quote
Old 02-02-2004, 03:20 AM   PM User | #7
egquen2000
New Coder

 
Join Date: Feb 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
egquen2000 is an unknown quantity at this point
Thanks a lot, you guys. I had already used your idea, Willy, before Glenn had a chance to post, and I don't really understand how your code works, Glenn. I follow it kind of, but I don't understand the namVal or the brackets with [0] and [1] following namVal. Would "namVal" go in place of the names I have, i.e. "requiredclientname"...? Could you explain that code to me, Glenn, in basic terms?
egquen2000 is offline   Reply With Quote
Old 02-02-2004, 03:35 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Maybe this is clearer...

var name, value, nameVal;
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('='); //returns an array with 2 items
name = unescape(nameVal[0].replace(/\+/g, " ")); //unescape item 1 from the array and replace +'s with spaces
value = unescape(nameVal[1].replace(/\+/g, " ")); //unescape item 2 from the array and replace +'s with spaces
params[name] = value; //put the name-value in "hash array"
}

and then you access the names like these:

var requiredclientname = params["requiredclientname"];
var requiredclientID = params["requiredclientID"];

Read the links I posted for more comprehensive discussion on this topic.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app

Last edited by glenngv; 02-02-2004 at 03:42 AM..
glenngv is offline   Reply With Quote
Old 02-02-2004, 03:59 AM   PM User | #9
egquen2000
New Coder

 
Join Date: Feb 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
egquen2000 is an unknown quantity at this point
Thumbs up

Thanks, Glenn

That IS a lot clearer. Boy, this site is a very big help. I had been trying my best to fix the problem myself to no avail. I'm glad you guys were there to help. Thanks again. Take care
egquen2000 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 +1. The time now is 09:45 AM.


Advertisement
Log in to turn off these ads.