NYCPHP Meetup

NYPHP.org

[nycphp-talk] Passing JAVASCRIPT variables to PHP

Kristina Anderson ka at kacomputerconsulting.com
Tue Apr 1 19:22:32 EDT 2008


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





More information about the talk mailing list