/    Sign up×
Community /Pin to ProfileBookmark

Passing Values question & example

I’m trying to use page to page query strings as in the example at:

[url]http://javascript.internet.com/forms/passing-values.html[/url]

However, this example doesn’t handle blank spaces well, the result is a “+” instead of a blank space, ie, “Ted Smith” entered results in “Ted+Smith”.

any quick-fix suggestions?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@herodote92Oct 02.2005 — I would do it like this (perhaps there is a more orthodox way):

In the getParams() function:
<i>
</i>params[nameVal[0]] = nameVal[1].replace(/+/g,' ');


This uses a RegExp to replace all '+' signs by blanks. What's interesting, in case you typed yourself a '+' in the field, it will be preserved (but this isn't the result of the function, it's related with the URL encoding).
Copy linkTweet thisAlerts:
@Stewart2authorOct 02.2005 — Can you explain how the "+" is related to "URL Encoding"

(but this isn't the result of the function, it's related with the URL encoding).[/QUOTE]

Have you tried the example I cited? did it insert a "+" in a typed 2-part spaced name?

Thanks
Copy linkTweet thisAlerts:
@Stewart2authorOct 02.2005 — That worked great!


So, replace(/+/g,' ') looks to me like it will remove "/", "", "+", but what is the "/g" ?
Copy linkTweet thisAlerts:
@herodote92Oct 02.2005 — Sure, I tried it before posting, in order not to look out like a fool ?

In the regexp:

- both slashes [COLOR=Red]/[/COLOR] are the delimiters of the /regexp/

- [COLOR=Red]+[/COLOR] means +, the backslash is here to escape (=protect from misinterpretation) the + sign; because + has a special signification inside a regexp

- the g following the 2nd slash means 'global' (replace globally, not only the 1st occurence)

When URL encoding, lots of characters, like the blank space, are replaced, in order that the URL will stay valid. I don't remember all the rules, but there should be easy to find on the Web.
Copy linkTweet thisAlerts:
@herodote92Oct 02.2005 — To get a better idea of it, I suggest you look at the parameter part (after the [COLOR=Red]?[/COLOR] sign) of the address shown by your browser after redirecting.

For example, if you typed:

[COLOR=SeaGreen]First name:[/COLOR] [FONT=Courier New]a+b c d[/FONT]

[COLOR=SeaGreen]Last name:[/COLOR] [FONT=Courier New]Smith[/FONT]

[COLOR=SeaGreen]Age:[/COLOR] [FONT=Courier New]18[/FONT]
[/quote]


then you should see in the address:

[COLOR=Red]?[/COLOR]firstname=a%2Bb+c+d&lastname=Smith&age=18
[/quote]


So you see that the [COLOR=Red]+[/COLOR] sign you typed has been encoded by [COLOR=Red]%2B[/COLOR], while both blanks have been figured by [COLOR=Red]+[/COLOR]. The [COLOR=Red]&[/COLOR] allows separating parameters (pairs name=value).

Of course, all this has to be unencoded again at the other end.
Copy linkTweet thisAlerts:
@Stewart2authorOct 03.2005 — Would you happen to know how to convert a Javascript value to an HTML input?

I'm trying to populate a HTML text or hidden input with a value from

javascript.

the Javascript values do display with this test:

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

business = unescape(params["business"]);

amount = unescape(params["amount"]);

item_name = unescape(params["item_name"]);

document.write("business = " + business + "<br>");

document.write("amount = " + amount + "<br>");

document.write("item_name = " + item_name + "<br>");

// End -->

</script>

HTML input:

<input type="text" name="business" value=business />

I can't find any examples or direction for this.

Any suggestions?
Copy linkTweet thisAlerts:
@Stewart2authorOct 03.2005 — I resolved it.
×

Success!

Help @Stewart2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...