Creating a querystring - prob with & turninging into &

L

Luklrc

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.
 
R

RobG

Luklrc said:
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.
 
R

RobG

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...
 
L

Luklrc

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 said:
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...
 
R

RobG

Luklrc said:
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.as...honenumber=a phone number&message=The message

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&vphonenumber=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>
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top