Passing JavaScript Data TO a Server Program (like PHP or whatever)

 

you have to understand that JavaScript is client side technology so even if inside PHP, the PHP does not have access to its
variables --because the PHP has already given its output by the time it reaches the client where the JavaScrip then executes.

WHAT DO YOU DO?  You need to send the data via a HTTP request to the Server Program with parameters in the request (name/value) pairs set to the data you are trying to send.


EXAMPLE SCENARIO:   you have a Facebook Tweets app and you want to have user login to Facebook and use the userID to look up
on your DataBase (data store/ data storage) on your server side their tweets.



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>
© Lynne Grewe