Passing JavaScript Data TO a Server Program (like PHP or whatever)
SOLTUIONS:
=======================
1) Option 1 through a link.
SCENARIO: suppose you have a link called "Get Tweets" and you want to pass the javascript variable "userID" to
that URL.
NOTE: your server code should expect the parameter (userid, ****) where *** is the value and get it from the
HTTP Request object your server code has access to. When user clicks on link it will go to the Server program and
pass the facebook userid and return the list of Tweets to the client.
Have somewhere in your JSP file the Link "Get Tweets"
<a href="#" id="tweet_link" onclick="return tweetURL();">Get Tweets </a>
<script type="text/javascript">
//code to get userid like say call to Facebook to get current userid if inside a facebook app
// THIS IS YOUR facebook code ending in storing userid in userid_var javascript variable
//code to build URL
function tweetURL(startURL) { document.getElementById("tweet_link").href="/whateverpath/TweetServerProgram?userid=" + userid_var ;
return false; } </script>
2) Option 2 (if you know how) use Javascript AJAX (see http://algebra.sci.csueastbay.edu/~grewe/CS3520/Mat/AJAX/index.htm)
SCENARIO: inside Javascript after you have gotten your userid make call to serverprogram via AJAX and pass as a parameter --
NOTE: in this scenario the USER DOES NOT HAVE OT CLICK ON ANYTHING
3) Option 3 : Like option #1 you alter something in HTML and hte user is clicking on something. Here the something is a submit button of a form and the javascript variable values are stored in hidden tags of form.
SCENARIO: the user clicks on the button "Start App Now"
NOTE: your server code should expect the parameter (userid, ****) where *** is the value and get it from the
HTTP Request object your server code has access to. When user clicks on link it will go to the Server program and
pass the facebook userid and return the list of Tweets to the client.
HTML
<from action="/whateverpath/ServerProgram" method="POST">
<input type="hidden" id="userid"/>
<input type="Submit" value="Start App Now">
</form
Javascript (in same page as HTML)
<script>
// THIS IS YOUR facebook code ending in storing userid in userid_var javascript variable
//now modify the hidden tag with user id you got above
document.getElementById("userid").value=userid_var;
</script>