0

Hello,

1. I have the following script:

function selectThis(id){
var locationstring = "process.php?value=" + id; /////
locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex");
locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex");
locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex");
locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex");
locationstring += "&in=" + $('#in_' + id).attr("selectedIndex");
document.location.href = locationstring;
}

With it I am getting a string with the value of the clicked button from an array and some other select field values that are listed in the same set of mysql results as the clicked button.

BUTTON:
<input name="button" type="button" onclick="selectThis('<? echo $rows; ?>');" value="">

At the end of the script the page is redirected to in the script mentioned page with following string:

process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6

(values are not real, so whatever the chosen value of the select field should apear in the link.)

THIS WORKS FINE.


2. My problem is following:

In order to be able to use var locationstring in other scripts as well I have removed var and have made it global.

function selectThis(id){
locationstring = "process.php?value=" + id; /////
locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex");
locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex");
locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex");
locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex");
locationstring += "&in=" + $('#in_' + id).attr("selectedIndex");
}

The problem is that later on when I use locationstring variable in other script I get only first variable in the string

process.php?value=1

instead of

process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6

3. Is it possible to make locationstring variable global and in a single line in order to be able to use the full string in other scripts or is there another solution.

Thank in advance.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Troy III
0

I got it:

function selectThis(id){
        var bs=document.getElementById("bs").selectedIndex;
        var ad=document.getElementById("ad_" + id).selectedIndex;
        var ns=document.getElementById("ns_" + id).selectedIndex;
        var wt=document.getElementById("wt_" + id).selectedIndex;
        var ins=document.getElementById("in_" + id).selectedIndex;
    locationstring = "?book=" + id + "&bs=" + bs + "&ad=" + ad + "&ns=" + ns + "&wt=" + wt + "&in=" + ins;
}

Edited by Nick Evan: Fixed formatting

0

Declaring variables with the keyword "var" outside of functions, makes a global var anyway;
But declaring it with 'var' keyword inside a body function makes it a local var.
therefore for internally initialized variables to be globally accessible you simply drop the variable constructor declaration.

This might also do just fine:
function selectThis(id){
locationstring = "process.php?value=" + id+
"&bs=" + $('#bs_' + id).attr("selectedIndex")+
"&ad=" + $('#ad_' + id).attr("selectedIndex")+
"&ns=" + $('#ns_' + id).attr("selectedIndex")+
"&wt=" + $('#wt_' + id).attr("selectedIndex")+
"&in=" + $('#in_' + id).attr("selectedIndex")+
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.