![]() ![]() |
Nov 7 2009, 01:58 PM
Post
#1
|
|
|
Newbie ![]() Group: Members Posts: 16 Joined: 10-March 09 Member No.: 29,318 |
Hi,
I'm working on a script where I need to send a query string to another page. This query string however is made up with several values that are added to the string conditionally. In PHP for example you can continue with variables like so: $var .= 'value'; $var .= 'value2'; echo $var; // result valuevalu2 How does this work with javascript? var Number = Number + 'whatever'; (?) Or does it have a similar method as in PHP? (A link where this is explained will suffice) Thank you. |
|
|
|
Nov 7 2009, 02:13 PM
Post
#2
|
|
|
Invested Member ![]() ![]() ![]() Group: Members Posts: 927 Joined: 18-March 09 From: EK, Scotland Member No.: 29,495 Languages: (X)HTML, CSS, JavaScript, PHP, MySQL [all are in their infancy!] |
You can use the + symbol to concatenate or += in the same fasion as .=
|
|
|
|
Nov 7 2009, 02:16 PM
Post
#3
|
|
|
Devoted Member ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 3,319 Joined: 16-January 08 Member No.: 19,529 |
The plus ( + ) sign is both an addition operator and a concatenation operator. So combining strings is as simple as the following:
var myKey = "name"; var myVal = "Bob"; var myQuery = myKey + "=" + myVal; // returns "name=Bob" The increment operator works also: myQuery += "&city=townsville"; // returns "name=Bob&city=townsville" But be wary. If you try to add numbers that still exist as strings (as when they come out of text inputs), they will not automatically be cast as numbers. You'll have to cast them yourself or force them to be cast: var i = 1; var s = "1"; var result = i + s; // returns "11"; result = i + Number(s); // returns 2 result = i + (s * 1); // returns 2 |
|
|
|
Nov 7 2009, 02:20 PM
Post
#4
|
|
|
Newbie ![]() Group: Members Posts: 16 Joined: 10-March 09 Member No.: 29,318 |
Using += like this:
var querystring += '&nPaginaKL='+nPaginaKL+'&nPaginaZW='+nPaginaZW+'&nPaginaZWatKL='+nPaginaZWatKL; gives me an 'invalid variable initialization'-error?? Edit: Got it now. I can't start with +=. I have to do var querysting = 'some value'. Then: querystring += 'somevalue'; This post has been edited by meddiecap: Nov 7 2009, 02:23 PM |
|
|
|
Nov 7 2009, 02:26 PM
Post
#5
|
|
|
Duotone Fox ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 3,584 Joined: 6-November 07 Member No.: 18,212 Languages: (X)HTML, CSS, Javascript, PHP, SQL, XML [DOM]. |
You need to assign something to your variable before concatenating to it. The way you're doing it, you're trying to append something to nothing, because querystring needs to be declared first.
CODE var querystring = ''; querystring += '&nPaginaKL='+nPaginaKL+'&nPaginaZW='+nPaginaZW+'&nPaginaZWatKL='+nPaginaZWatKL; Alternatively, there's the String.concat() method: CODE var querystring = new String();
querystring.concat('&nPaginaKL=', nPaginaKL, '&nPaginaZW=', nPaginaZW, '&nPaginaZWatKL=', nPaginaZWatKL); This post has been edited by Ingolme: Nov 7 2009, 02:26 PM |
|
|
|
Nov 7 2009, 02:29 PM
Post
#6
|
|
|
Devoted Member ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 3,319 Joined: 16-January 08 Member No.: 19,529 |
(IMG:style_emoticons/default/blush.gif) Sorry. I meant to highlight the traps. Forgot that one.
|
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 23rd November 2009 - 12:17 AM |