1

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
1
  • Would appreciate if you could post more code. How you define the variables. And how you call the function. Best from Jonas Commented Jan 30, 2012 at 8:25

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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>

see the example in fiddle

answered Jan 30, 2012 at 8:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.