473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?valu e1=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.realn ame.value
var vemail =fmContact.emai l.value
var vphonenumber = fmContact.phone number.value
var vmessage = fmContact.messa ge.value
fmContact.missi ng_fields_redir ect.value =
"http://www.mywebsite.c om/contact.asp?mis sing=1" & "&realname= " &
vname & "&email=" & vemail & "&phonenumb er=" & vphonenumber &
"&message=" & vmessage

}

Thanks in advance.
Jul 23 '05 #1
4 3026
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?valu e1=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.repla ce(/&/,'&'));

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

Heres my code :

function setvalue(){
var vname = fmContact.realn ame.value
var vemail =fmContact.emai l.value
var vphonenumber = fmContact.phone number.value
var vmessage = fmContact.messa ge.value
fmContact.missi ng_fields_redir ect.value =
"http://www.mywebsite.c om/contact.asp?mis sing=1" & "&realname= " &
vname & "&email=" & vemail & "&phonenumb er=" & 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.missi ng_fields_redir ect.value =
'http://www.mywebsite.c om/contact.asp?mis sing=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.missi ng_fields_redir ect.value =


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

document.fmCont act.missing_fie lds_redirect.va lue

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.fmCont act.missing_fie lds_redirect.va lue = encodeURI(
'http://www.mywebsite.c om/contact.asp?mis sing=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 programmaticall y. Does anyone have any other
ideas?


RobG <rg***@iinet.ne t.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.missi ng_fields_redir ect.value =


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

document.fmCont act.missing_fie lds_redirect.va lue

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.fmCont act.missing_fie lds_redirect.va lue = encodeURI(
'http://www.mywebsite.c om/contact.asp?mis sing=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=T he+vname&vemail =an+email%3F&vp honenumber=a+ph one+number&vmes sage=The+messag e

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

<script type="text/javascript">

function checkString(f) {
f.missing_field s_redirect.valu e = encodeURI(
'http://www.mywebsite.c om/contact.asp?mis sing=1'
+ '&realname=' + f.vname.value
+ '&email=' + f.vemail.value
+ '&phonenumber =' + f.vphonenumber. value
+ '&message=' + f.vmessage.valu e
);
}

function newLoc(f) {
checkString(f);
document.locati on = f.missing_field s_redirect.valu e;

// 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="retur n newLoc(this);">
<input type="text" name="vname" value="The vname"><br>
<input type="text" name="vemail" value="an email?"><br>
<input type="text" name="vphonenum ber" value="a phone number"><br>
<input type="text" name="vmessage" value="The message"><br>
<input type="button" value="Check" onclick="
checkString(thi s.form);
">
<input type="submit">< br>
<input type="text" name="missing_f ields_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 programmaticall y. 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
3647
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 rules when it comes to file paths. A lot of Microsoft installers for example, and also installers of other companies, do not work because they handle paths in the following manner:
0
1652
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 under IIS. The problem I get is when I create an XmlElement with an XmlAttribute whose value contains the "&" character. When the xml is written the "&" has become "&amp;amp". This happens when I use XmlDocument.InnerXML or...
16
2470
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 could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
1
1437
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" cellspacing="0" cellpadding="0"> <tr> <td width="20%"> <p>blah blah blah</p>
3
1249
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. Everything looks fine but we can read the letter "H" and not the entire string. I'm sure that the problem is the BSTR variable but I do not know what else to do so, if somebody knows what do I need to do in order to read the entire string please...
7
2365
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 VB example I found on the web. I would be most appreciative if someone could explain why this statement is necessary and what does it do? MyArt = New Art ' **** ??????? ****
3
11901
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( & ", " & )'. It works fine at other clients. How can I resolve this? I do not see anything in the MS KB.
3
4764
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 something goes wrong. The problem at hand: In a restricted area I let a user upload an image, no problem The image gets scaled down with imagecopyresampled(), and stored with imagejpeg($resized_img,'/path/to/target/image.jpg')
4
3724
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, if I need to add more parameters at a later time, may I then use something like: www.mysite.com/article/sports/football/?id=321&language=EN
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.