Following is my javascript code. The problem is when I call the send() function it doesnt really respond because x, y and z because they are local variables. I have tried making them global using a few approaches like creating an object but all have failed. Anybody here knows a proper approach to do that?
<script type='text/javascript'>
function store(x,y,z) {
var date = x+" "+y+" "+z;
document.getElementById("date").value=date;
}
function send(){
var events=document.getElementById("event").value;
location.href="q.php?day=" + x + "&month=" + y + "&year=" + z;
}
</script>
Amar Palsapure
9,6561 gold badge30 silver badges46 bronze badges
asked Jan 30, 2012 at 8:22
Parth Mody
4661 gold badge6 silver badges18 bronze badges
-
Would appreciate if you could post more code. How you define the variables. And how you call the function. Best from JonasJonas m– Jonas m2012年01月30日 08:25:16 +00:00Commented Jan 30, 2012 at 8:25
3 Answers 3
Try this, considering you are calling store function before send
<script type='text/javascript'>
var xx, yy, zz; //These are your global variables
function store(x,y,z) {
xx = x;
yy = y;
zz = z;
var date = x + " " + y + " " + z;
document.getElementById("date").value = date;
}
function send(){
var events = document.getElementById("event").value;
location.href="q.php?day=" + xx + "&month=" + yy + "&year=" + zz;
}
</script>
answered Jan 30, 2012 at 8:24
Amar Palsapure
9,6561 gold badge30 silver badges46 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can create the URL in the store function, then you only need one global variable:
<script type='text/javascript'>
var url;
function store(x, y, z) {
var date = x + " " + y + " " + z;
document.getElementById("date").value = date;
url = "q.php?day=" + x + "&month=" + y + "&year=" + z;
}
function send(){
var events = document.getElementById("event").value;
location.href = url;
}
</script>
answered Jan 30, 2012 at 8:39
Guffa
703k112 gold badges760 silver badges1k bronze badges
Comments
change the name of function send and store
<script type='text/javascript'>
var xGlobal,yGlobal,zGlobal;
function mystore(x,y,z) {
xGlobal=x;
yGlobal=y;
zGlobal=z;
var date = x+" "+y+" "+z;
document.getElementById("date").value=date;
}
function mysend(){
var events=document.getElementById("event").value;
location.href="q.php?day=" + xGlobal + "&month=" + yGlobal + "&year=" + zGlobal;
}
</script>
answered Jan 30, 2012 at 8:33
Hemant Metalia
30.8k18 gold badges77 silver badges93 bronze badges
Comments
lang-js