NYCPHP Meetup

NYPHP.org

[nycphp-talk] Passing JAVASCRIPT variables to PHP

Kristina Anderson ka at kacomputerconsulting.com
Wed Apr 2 16:33:53 EDT 2008


Paul,

Basically (at least IMHO), the best way for you to start thinking about 
solving problems of how to pass variables from let's say a document 
object back to the server to a PHP script (for instance the user system 
date & time as recorded by the document object as in your example) is 
for you to think in terms of using forms -- you can store your user's 
system date & time in the hidden field(s) and then when they submit the 
form, record it in the database, and it will be accessible to your 
server-side script after form submit.

The good part of hidden fields is it's easy to view the source and make 
sure your variables are where they should be.  The bad part is, 
obviously, you don't want to use them to store anything sensitive, 
confidential or that otherwise you would not want some savvy user who 
knows how to view document source to read, because they will be in 
there.

This is exactly the type of thing that I recall years ago trying to 
wrap my head around when I first started out.  (Now, I'm reduced to 
missing stupid bugs when I mistype a query name!!).

Kristina

> I am really sorry for being so utterly thick; but I still just do not 
get 
> it. I would be so grateful if you have any coded examples that I 
could look 
> at and follow. while I really value what everybody has said most of 
it was 
> just beyond my understanding.
> 
> Paul
> 
> ----- Original Message ----- 
> From: "Kristina Anderson" <ka at kacomputerconsulting.com>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Sent: Wednesday, April 02, 2008 12:22 AM
> Subject: Re: [nycphp-talk] Passing JAVASCRIPT variables to PHP
> 
> 
> > One of the projects I inherited recently contains the following code
> > which is used basically to store the record ID and editstate in the
> > URL's querystring.  When I first saw it, I was scratching my head
> > saying, why would anyone think they needed to do it this way?
> >
> > (My preferred way to store data such as this would be in hidden 
fields
> > which are submitted with the form, but I do realize that everyone 
has
> > different coding styles.  And mind you, they do have hidden fields
> > which do contain IDs for other purposes...)
> >
> > The script in the document head is
> >
> > <script language="javascript">
> > function jfunc(arg){
> > window.location='thissamedocument.php?id='+arg+'&edit=false';
> > }
> > </script>
> >
> > and then throughout the page this script is called by echoing the
> > following:
> >
> >                echo "<SCRIPT LANGUAGE='JavaScript'>\n";
> > echo "javascript: jfunc(" . $whateverID . ")\n";
> > echo "</script>\n";
> >
> > Would love to hear any and all comments on the above vis a vis this
> > discussion.
> >
> > --Kristina
> >
> >
> >> Susan hit the nail on the head. She's pointing out why I said it 
was a
> >> "hack" - if you're not understanding the problem correctly (the
> >> difference between client-side and server-side) then the proposed
> >> solution might be "simple and workable" but it's still wrong.
> >>
> >> More to the point, what exactly are your goals with this code, 
Paul?
> >> Are you just trying to get the exact time on the client's computer?
> >> Are you just trying to create a timestamp in their timezone?
> >>
> >> On Mon, Mar 31, 2008 at 6:51 PM, Susan Shemin
> > <susan_shemin at yahoo.com> wrote:
> >> >
> >> >
> >> >
> >> > I'm watching this discussion with interest since I asked a 
similar
> > question
> >> > last month (about sending PHP stats from a JS onclick event).  
The
> > answer
> >> > that came up was to put a redirect to the link page, run the PHP
> > script on a
> >> > redirect.php page and then send it on to the destination.
> >> >
> >> >
> >> >
> >> > I've set it up this way and it's working fantastically, but I 
have
> > tons of
> >> > links and I'm beginning to feel hesitant about sending users to a
> > redirect
> >> > when there's so many harmful redirects out there.  (Of course, 
not
> > mine...)
> >> >
> >> >
> >> >
> >> > Just as this question came up here, I was again researching it on
> > the
> >> > internet, and very clearly saw that the crux of the problem is 
that
> >> > Javascript is client side and PHP server side, meaning the 2 
don't
> > mix
> >> > unless in Ajax.
> >> >
> >> >
> >> >
> >> > So I'm off to brush up on my Ajax and get it working, because
> > except for the
> >> > redirect, I can only see that Ajax will work.
> >> >
> >> >
> >> >
> >> > Susan
> >> >
> >> >
> >> > _______________________________________________
> >> >  New York PHP Community Talk Mailing List
> >> >  http://lists.nyphp.org/mailman/listinfo/talk
> >> >
> >> >  NYPHPCon 2006 Presentations Online
> >> >  http://www.nyphpcon.com
> >> >
> >> >  Show Your Participation in New York PHP
> >> >  http://www.nyphp.org/show_participation.php
> >> >
> >>
> >>
> >>
> >> -- 
> >> realm3 web applications [realm3.com]
> >> freelance consulting, application development
> >> (917) 512-3594
> >> _______________________________________________
> >> New York PHP Community Talk Mailing List
> >> http://lists.nyphp.org/mailman/listinfo/talk
> >>
> >> NYPHPCon 2006 Presentations Online
> >> http://www.nyphpcon.com
> >>
> >> Show Your Participation in New York PHP
> >> http://www.nyphp.org/show_participation.php
> >>
> >>
> >
> >
> > _______________________________________________
> > New York PHP Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > NYPHPCon 2006 Presentations Online
> > http://www.nyphpcon.com
> >
> > Show Your Participation in New York PHP
> > http://www.nyphp.org/show_participation.php 
> 
> 
> 



More information about the talk mailing list