473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Creating a querystring - prob with & turninging into &

Hi,

I'm having to create a querysting with javascript. My problem is that
javscript turns the "&" characher into "&" when it gets used as a
querystring in the url EG:

/mypage.asp?value1=1&value2=4& ...

which of course means nothing to asp.

I know in vbscript you can force a "&" to be outputed using something
like chr(38). Is there something similar for javascript? I cant find
mention of it anywhere! Or any alternatives, or something i have
missed? I'm completely stuck!

Heres my code :

function setvalue(){
var vname = fmContact.realname.value
var vemail =fmContact.email.value
var vphonenumber = fmContact.phonenumber.value
var vmessage = fmContact.message.value
fmContact.missing_fields_redirect.value =
"http://www.mywebsite.com/contact.asp?missing=1" & "&realname=" &
vname & "&email=" & vemail & "&phonenumber=" & vphonenumber &
"&message=" & vmessage

}

Thanks in advance.
Jul 23 '05 #1
4 2993
Luklrc wrote:
Hi,

I'm having to create a querysting with javascript. My problem is that
javscript turns the "&" characher into "&" when it gets used as a
querystring in the url EG:

/mypage.asp?value1=1&value2=4& ...

which of course means nothing to asp.

I know in vbscript you can force a "&" to be outputed using something
like chr(38). Is there something similar for javascript? I cant find
mention of it anywhere! Or any alternatives, or something i have
missed? I'm completely stuck!
var txt = 'this contains an & character'
alert(txt.replace(/&/,'&'));

Will do the trick but it's not necessary (see below).

Heres my code :

function setvalue(){
var vname = fmContact.realname.value
var vemail =fmContact.email.value
var vphonenumber = fmContact.phonenumber.value
var vmessage = fmContact.message.value
fmContact.missing_fields_redirect.value =
"http://www.mywebsite.com/contact.asp?missing=1" & "&realname=" &
vname & "&email=" & vemail & "&phonenumber=" & vphonenumber &
"&message=" & vmessage


It seems you are allowing autowrap to do your wrapping - don't. Wrap
code manually at about 70 characters or you will get wrapping errors
in posted code that obfuscate the real errors (if any).

You are using '&' as a string concatenation operator, but it isn't.
The correct symbol is '+'.

fmContact.missing_fields_redirect.value =
'http://www.mywebsite.com/contact.asp?missing=1'
+ '&realname=' + vname
+ '&email=' + vemail
+ '&phonenumber=' + vphonenumber
+ '&message=' + vmessage
;

That should do the trick.

--
Rob
Jul 23 '05 #2
RobG wrote:
[...]
You are using '&' as a string concatenation operator, but it isn't.
The correct symbol is '+'.

fmContact.missing_fields_redirect.value =


While the above will work in IE and Firefox, it really should be:

document.fmContact.missing_fields_redirect.value

or more formally:

document.forms['fmContact'].elements['missing_fields_redirect'].value

and if that is going to be used as a URI, it should also have:

... = encodeURI( ... )

because some of the characters entered may required it. So the full
snippet is

document.fmContact.missing_fields_redirect.value = encodeURI(
'http://www.mywebsite.com/contact.asp?missing=1'
+ '&realname=' + vname
+ '&email=' + vemail
+ '&phonenumber=' + vphonenumber
+ '&message=' + vmessage
);

[...]

But it seems that should really be inserted into a href anyway...
--
Rob
Jul 23 '05 #3
Thanks.

I've tried what you suggested but I've still got the same problem.
Tried it in IE, firefox and another PC so its not a local setting. Any
other suggestions?

The reason I have to do it like this is because I have to use a 3rd
party form to mail, which requires a redirect url if a required field
is missing. If there is a required field missing I want to take the
users back to the form with the values still in it, and display the
appropriate error messages. The only way I can figure to do this is to
create the querystring programmatically. Does anyone have any other
ideas?


RobG <rg***@iinet.net.auau> wrote in message news:<4a****************@news.optus.net.au>...
RobG wrote:
[...]
You are using '&' as a string concatenation operator, but it isn't.
The correct symbol is '+'.

fmContact.missing_fields_redirect.value =


While the above will work in IE and Firefox, it really should be:

document.fmContact.missing_fields_redirect.value

or more formally:

document.forms['fmContact'].elements['missing_fields_redirect'].value

and if that is going to be used as a URI, it should also have:

... = encodeURI( ... )

because some of the characters entered may required it. So the full
snippet is

document.fmContact.missing_fields_redirect.value = encodeURI(
'http://www.mywebsite.com/contact.asp?missing=1'
+ '&realname=' + vname
+ '&email=' + vemail
+ '&phonenumber=' + vphonenumber
+ '&message=' + vmessage
);

[...]

But it seems that should really be inserted into a href anyway...

Jul 23 '05 #4
Luklrc wrote:
Thanks.

I've tried what you suggested but I've still got the same problem.
Tried it in IE, firefox and another PC so its not a local setting. Any
other suggestions?
Please don't top post. Reply directly below the text you are
replying to and trim any excess content.

Below is a script that does exactly what (I think) you want.
checkString() builds the 'query string' and puts it into the
missing_fields input (which is disabled so it doesn't get posted).

When submit is clicked, onsubmit runs newLoc, which runs checkString
to put the value into 'missing_fields', then uses the content of
'missing_fields' as the new URI.

Using the default values, when checkString is run, missing_fields is
given the following value (please excuse wrapping):
http://www.mywebsite.com/contact.asp...=The%20message

When submit is clicked, the above value is written to missing_fields
by checkString, then newLoc posts the form with the following URI:

..../z2.html?vname=The+vname&vemail=an+email%3F&vphonen umber=a+phone+number&vmessage=The+message

which seems to have ampersand (&) characters in it exactly where you
wanted them.

<script type="text/javascript">

function checkString(f) {
f.missing_fields_redirect.value = encodeURI(
'http://www.mywebsite.com/contact.asp?missing=1'
+ '&realname=' + f.vname.value
+ '&email=' + f.vemail.value
+ '&phonenumber=' + f.vphonenumber.value
+ '&message=' + f.vmessage.value
);
}

function newLoc(f) {
checkString(f);
document.location = f.missing_fields_redirect.value;

// If this was a validation script and validation had failed,
// I would return false from this script and give the user
// some hints on fixing the form.

// If validation is OK, then don't return anything (or
// return true if you really must) and the form will submit.

// The redirect above means that control never returns to the
// form and the new URI is loaded.

}
</script>

<form action="" name="fmContact" onsubmit="return newLoc(this);">
<input type="text" name="vname" value="The vname"><br>
<input type="text" name="vemail" value="an email?"><br>
<input type="text" name="vphonenumber" value="a phone number"><br>
<input type="text" name="vmessage" value="The message"><br>
<input type="button" value="Check" onclick="
checkString(this.form);
">
<input type="submit"><br>
<input type="text" name="missing_fields_redirect" size="100" disabled>
</form>

The reason I have to do it like this is because I have to use a 3rd
party form to mail, which requires a redirect url if a required field
is missing. If there is a required field missing I want to take the
users back to the form with the values still in it, and display the
appropriate error messages. The only way I can figure to do this is to
create the querystring programmatically. Does anyone have any other
ideas?


Normally in-page validation is done to save a trip to the server.
If validation fails, give the user a message with hints on what to
fix and return false from your onsubmit validation script to cancel
sending the form.

If the user does not have JS or it is disabled, then the invalid form
data will be sent without any client-side processing.

What you seem to be trying to do is generate exactly the URI that
would be sent by the form if you'd submitted it without any
JavaScript at all. So what's the point?
--
Rob
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
0
by: James Thurley | last post by:
I'm creating an XmlDocument manually, adding content using the Xml classes such as XmlElement and XmlText, and I then write it out as as "text/xml" to the HttpResponse.Output TextWriter object...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
1
by: josquin | last post by:
Hello, I am creating a page that features a large table with relative sizes, where the images therein are also relative in size, as in the following: <table width="100%" border="0"...
3
by: RobertoPerez | last post by:
We have a default "Hello World" web services that we can call from VC++ creating an unmanaged DLL. The DLL is used in old languages that calls the DLL-function and that is calling the web sevice....
7
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the statement that solved the problem is necessary. The inspiration for the statement came from an undocumented...
3
by: pw | last post by:
Hi, I created and distributed an Access 2003 MDE. When the user opens up a form he get's an error message :Function is not available in expressions in query expression 'Trim( & ", " & )'. ...
3
by: Rik | last post by:
Hello, first of all, my provider sucks, newsserver is down for the #nth time now, offcourse when I have an urgent question.... So this will be me first time using Google Groups, forgive me if...
4
by: royend | last post by:
Hi. Is is possible to pass parameters with this URL: www.mysite.com/article/sports/football instead of the usual method: www.mysite.com/article.asp?category=sports&subcategory=football And,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.