Click to See Complete Forum and Search --> : Javascript & MySQL


The Anime King
05-22-2005, 12:15 PM
Every visitor on my site gets an automatic MySQL table with stats like screen size, ip, browser etc.

Now everything is saved in the user table, but there's a little problem.
How can I update a javascript code like screen.width?
Update user SET screen=screen.width WHERE ip=$ip;
That's not working... Is there a PHP code for it? Or how can I fix it?
Greetz tAK

grailquester5
05-22-2005, 03:06 PM
JavaScript is a client-side language, PHP/MySQL are server-side, hence they don't speak directly to one another. The only way (I know of) to pass values from JavaScript to PHP is to create an invisible form and populate it with the information that you'd like to pass (or pass it in a URL as in a GET statement and then use PHP to interpret the GET).

bokeh
05-22-2005, 03:21 PM
JavaScript is a client-side language, PHP/MySQL are server-side, hence they don't speak directly to one another. The only way (I know of) to pass values from JavaScript to PHP is to create an invisible form and populate it with the information that you'd like to pass (or pass it in a URL as in a GET statement and then use PHP to interpret the GET).
Actually javascript can send information to php behind the scenes without a new page load being necessary.

felgall
05-22-2005, 03:27 PM
Actually javascript can send information to php behind the scenes without a new page load being necessary.

How can it do that when the PHP has finished running before the page reaches the browser and the Javascript doesn't start running until after that and so doesn't even know that there is PHP there to interact with?

You can embed PHP into Javascript so that the PHP can generate the Javascript content but the other way around - I don't think so.

grailquester5
05-22-2005, 03:40 PM
bokeh, I'm sorry, but no it can't. felgall is right - PHP runs first, on the server, and sends data to the browser. PHP can be used to create Javascript (just like HTML), but not vice-versa.

PHP has 3 ways (one which I didn't mention previously) of getting data from Javascript:

1. Use Javascript to set a cookie and retrieve the cookie on another page using PHP.

2. Append variables to a URL with Javascript, then use Javascript to redirect the browser to said URL (or have the user redirect it).

3. Have Javascript set the value(s) of hidden input fields in a form and have the user submit the form.

That's all there is to it.

The Anime King
05-22-2005, 03:43 PM
I think I'm going to solve it with this line:
<iframe src=stats.php?screen=<script>document.write(screen.width+"x"+screen.height);</script>&colors=<script>document.write(screen.colorDepth);</script>></iframe>

But now I have another question. Is there a way to auto-refresh PHP code?
Let's say Online() is a function that reads the SQL-database for online visitors, but after ten minutes of reading the info, maybe all visitors are gone and still it gives the same number... Is there a way to autorefresh only a little bit of PHP-code?

Greetz tAK

bokeh
05-22-2005, 03:50 PM
I'm sorry my friends but your technical knowledge is out of date. Full bi-directional communication is possible between javascript and the server (behind the scenes, ie no page loads being involved) with all modern browsers except Opera. If you dont believe me take a look at this (http://developer.apple.com/internet/webcontent/xmlhttpreq.html) and this (http://www.xml.com/pub/a/2005/02/09/xml-http-request.html) and this (http://www.modernmethod.com/sajax/). The XMLHttpRequest Object

This object, first implemented by Microsoft as an ActiveX object but now also available as a native object within both Mozilla and Apple's Safari browser, enables JavaScript to make HTTP requests to a remote server without the need to reload the page. In essence, HTTP requests can be made and responses received, completely in the background and without the user experiencing any visual interruptions.

grailquester5
05-22-2005, 04:11 PM
bokeh, that's great - but that's XML - invoking an XML object (XMLHttpRequest). And although it can make use of Javascript and PHP, it is NEITHER Javascript NOR PHP. Hence - for those of us who don't know/use XML (yet), it is useless. The question was can Javascript send information to PHP? The answer is NO. XML, however, via their new object (XMLHttpRequest) can maintain an open connection and invoke Javascript on the client side and PHP on the server side (which IS great) - but again - it is neither PHP nor Javascript DOING this.

bokeh
05-22-2005, 04:17 PM
That's like saying a client cannot communicate directly with a server because the internet is a middleman between the two.

The Anime King
05-22-2005, 04:17 PM
Okey, I'm not using XML, I'm not gonna learn ANOTHER language... got twenty of 'm already... anyway is this possible:
But now I have another question. Is there a way to auto-refresh PHP code?
Let's say Online() is a function that reads the SQL-database for online visitors, but after ten minutes of reading the info, maybe all visitors are gone and still it gives the same number... Is there a way to autorefresh only a little bit of PHP-code?

grailquester5
05-22-2005, 07:09 PM
King,

If you're using an iframe, you could (I think - never having tried it) just reload that iframe (the frame's source). Javascript has a setTimeout() function which you could use in a loop to reload the content of said iframe, which would obviously be your "visitors.php" file or something...

bokeh,

Not even going to respond to that.

BurntHand
10-25-2007, 04:14 AM
Wikipedia Quote:

http://en.wikipedia.org/wiki/Ajax_(programming)

"XML is sometimes used as the format for transferring data between the server and client, although any format will work, including preformatted HTML, plain text and JSON. These files may be created dynamically by some form of server-side scripting."

bokeh
10-25-2007, 09:18 AM
Please don't reply to threads that are 2 1/2 years old already.