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

3 Answers 3

2

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>
0
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>
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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.