Forum Moderators: open

Message Too Old, No Replies

re: Function to build Query String with Javascript

javascript Query String

         

nadavvin

10:05 am on Nov 15, 2007 (gmt 0)

10+ Year Member



The thread:
[webmasterworld.com...]

is closed, so I write here:

The password field isn't handle by the script, I added:
case "password":


function create_request_string(theform)
{
var reqStr = "";

for(i=0; i < theform.elements.length; i++)
{
isformObject = false;

if (theform.elements[i].tagName == "INPUT")
{
switch (theform.elements[i].type)
{
case "text":
case "hidden":
case "password":
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
isformObject = true;
break;

case "checkbox":
if (theform.elements[i].checked)
{
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
}else{
reqStr += theform.elements[i].name + "=";
}
isformObject = true;
break;

case "radio":
if (theform.elements[i].checked)
{
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
isformObject = true;
}
}
}

if (theform.elements[i].tagName == "SELECT")
{
var sel = theform.elements[i];
reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
isformObject = true;
}

if ((isformObject) && ((i+1)!= theform.elements.length))
{
reqStr += "&";
}

}

return reqStr;
}

Fotiman

3:12 pm on Nov 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's a slightly better formatted version. I also made some minor modifications. Note, you might want to consider adding a test to make sure the inputs have a 'name' property before you try and it use it in your query string.


function create_request_string(theform) {
var reqStr = "";
var el, i, isformObject;
for(i = 0; i < theform.elements.length; i++) {
isformObject = false;
el = theform.elements[i];
if (el.tagName == "INPUT") {
switch (el.type) {
case "text":
case "hidden":
case "password":
reqStr += el.name + "=" + el.value;
isformObject = true;
break;
case "checkbox":
if (el.checked) {
reqStr += el.name + "=" + el.value;
} else {
reqStr += el.name + "=";
}
isformObject = true;
break;
case "radio":
if (el.checked) {
reqStr += el.name + "=" + el.value;
isformObject = true;
}
break;
}
}
if (el.tagName == "SELECT") {
reqStr += el.name + "=" + el.options[el.selectedIndex].value;
isformObject = true;
}
if ((isformObject) && ((i + 1)!= theform.elements.length)) {
reqStr += "&";
}
}
return reqStr;
}

Dabrowski

3:05 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Foti, how do you make code appear with proper tabulation?

Fotiman

3:47 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month


I surround my code examples with [quote] and [pre], and then make sure there are no blank lines (that screws up the formatting). For example, I might post something like this:

[quote][pre]
function foo() {
....alert('foo');
....// No blank lines...
}
[/pre][/quote]

Note, replace the . with spaces (since I'm disabling codes for this message, the above example will not be formatted, so I used . to represent my leading whitespace). Also note that this BB will still trim down the spaces. For every 2 whitespace characters, only 1 will show up. So if I use 4 spaces, it gets published as 2 spaces. If I then edit that message, it will only have 2 spaces where there was once 4, so if I save it, then I'll only have 1 space... so you need to use care when editing a message you already posted.
:-)

[1][[b]edited by[/b]: Fotiman at 3:51 pm (utc) on Nov. 16, 2007][/1]

Dabrowski

3:53 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh I see. Doesn't it annoy you that in a Forum about code, there's no good way to represent code? In the Perl forum you can use [perl] and it does syntax highlight and everything else!

Fotiman

4:07 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, it does bother me. There is a 'code' style code, but it doesn't work well (doesn't preserve whitespace, doesn't handle blank lines, etc.).

For example:


function foo() {
alert('foo');

alert('bar');
}

Dabrowski

9:25 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From what I've seen, fixed and code are the same. Both not very helpful. I tried using pre on another thread today, even that truncates some whitespace.