469,097 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

Passing javascript variables to Perl

12
Hi

I have a html page with javascript in it that assigns a set of coordinates to javascript variables. The question I have is how can I then send these variables to a Perl CGI script using a submit on a html form.

An example of some of the code below:

Expand|Select|Wrap|Line Numbers
  1.     var map;
  2.     var geocoder = null;
  3.     var addressMarker;
  4.     var originpoint = null;
  5.  
  6. ....................
  7.  
  8.  
  9. function showAddress(address) {
  10.       if (geocoder) {
  11.         geocoder.getLatLng(address,
  12.           function(point1) {
  13.             if (!point1) {
  14.               alert(address + " not found");
  15.             } else {
  16.               if (addressMarker) {
  17.                 map.removeOverlay(addressMarker);
  18.               }
  19.               addressMarker = new GMarker(point1);
  20.               map.setCenter(point1, 15);
  21.               map.addOverlay(addressMarker);
  22.               originpoint = point1;
  23.               alert("origin is now: "+originpoint+" destination is now: "+destinationpoint);
  24.             }
  25.           }
  26.         );
  27.       }
  28.     }
  29.  
  30. <body onload="load()" onunload="GUnload()">
  31.     <form action="#" onsubmit="showAddress(this.address.value); return false">
  32.       <p>
  33.  Origin:<input type="text" size="50" id="addressInput" name="address" value="London, UK" />
  34.         <input type="submit" value="Show Origin" />
  35.       </p>
  36.     </form>
  37.  
  38.  
Now I want to be able to store the variable "originpoint" and using another form to submit it to the perl CGI script. Any ideas?
Jul 20 '07 #1
8 4996
Harch84
12
Can anyone suggest an answer to this? I was thinking about some sort of hidden input form but am not sure how to implement it or how it would work?
Jul 20 '07 #2
pbmods
5,821 Expert 4TB
Heya, Harch.

Easiest way to do it is to put them in the form. Create a couple of hidden inputs, then assign their values in the form's onsubmit.
Jul 21 '07 #3
Harch84
12
Hi Pbmods

I tried to do what you said but seem to be passing just the name of the variable not the actual value of the variable?

Expand|Select|Wrap|Line Numbers
  1.     <form action="http://morar.geos.ed.ac.uk/~s0679212/cgi_bin/sql_test.pl" method="POST" onSubmit="Origin">
  2.     <input type="hidden" name="Origin" value="originpoint">
  3.     <input type="Submit" value="Go!">
  4.     </form>
  5.  
In the perl script this returns the text "originpoint" instead of the actual value of the variable originpoint from javascript. What am I doing wrong?
Jul 21 '07 #4
pbmods
5,821 Expert 4TB
Heya Harch.

You have to use the onsubmit handler for your form and then use JavaScript to populate the form values:

Expand|Select|Wrap|Line Numbers
  1. <form ... onsubmit="return setVars();">
Expand|Select|Wrap|Line Numbers
  1. function setVars()
  2. {
  3.     document.getElementById('idOfInput') = variableName;
  4.     .
  5.     .
  6.     .
  7.     return true;
  8. }
  9.  
Jul 21 '07 #5
Harch84
12
Im really sorry about this but I still seem to be getting errors in my script preventing the javascrip variable to be passed to perl. I have used the advice given here and have this code:

Expand|Select|Wrap|Line Numbers
  1.     <script type="text/javascript">
  2.     //<![CDATA[
  3.  
  4.     function setVars()
  5.         {
  6.             document.getElementById('orig') = originpoint;
  7.             return true;
  8.         }
  9.  
  10.     //]]>
  11.     </script>
  12.  
  13.  
  14. <body onload="load()" onunload="GUnload()">
  15.  
  16. ........................ // other forms go here that use geocoder
  17.  
  18. <form action="http://morar.geos.ed.ac.uk/~s0679212/cgi_bin/sql_test.pl" method="POST" onsubmit="return setVars();">
  19. <input type="hidden" id="orig" name="Origin">
  20. <input type="Submit" value="Go!">
  21. </form>
  22.  
  23.   </body>
  24. </html>
  25.  
  26.  
Now the error I get is when I click on the submit button on the web page and it says that on the line 90 char 4 which is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 88    function setVars()
  3. 89    {
  4. 90        document.getElementById('orig') = originpoint;
  5. 91        return true;
  6. 92    }
  7.  
  8.  
The error says that there is a "Wrong number of arguements or invalid property assignment".??

As you can tell im no javascript expert so any help would be massivly appreciated and im sure its just something simple that I cant see but is obvious to you all. Thanks in advance
Jul 22 '07 #6
pbmods
5,821 Expert 4TB
Heya, Harch.

You're getting close.

In that statement, you are trying to set the *element* to originpoint, which is what's giving JavaScript such a bellyache.

Instead, you want to set the element's *value* to originpoint:
Expand|Select|Wrap|Line Numbers
  1.  function setVars()
  2. {
  3.     document.getElementById('orig').value = originpoint;
  4.     return true;
  5. }
  6.  
Jul 22 '07 #7
Harch84
12
Heya Pbmods

That was the final part of the puzzle thanks so much for your help! Works perfectly now I almost cant believe it :-)

All the best

Harch
Jul 22 '07 #8
pbmods
5,821 Expert 4TB
Heya, Harch.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Jul 22 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by google_nospam | last post: by
1 post views Thread by Consuelo Guenther | last post: by
3 posts views Thread by domeceo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.