08-28-2007, 04:19 AM
I realized an issue with a portion of a project I am working on, I made a counter that keeps track of how many views a collapsed/hidden section receives via a mouse click on a link that makes that section visible. It turns out it increases the number with each page load rather then only once as I was wanting. I have since realized that PHP runs at loading and there does not seem to be anything you can do about it, though is there a way around this like calling the PHP from an external file once the JS function is activated and passing the variable back? TIA
08-28-2007, 04:52 AM
Perhaps some PHP 101 will help.
Specifically, here is what happens with the code you posted each time it is requested/refreshed -
If we assume that that is all the code on the page, when the the last line on the page has been output to the browser, all the resources that were used by the page are destroyed, the web server forgets that it just served up one of your pages (except for what might have been written to a log file) and the web server goes on to serve other requests.
08-28-2007, 05:09 AM
Thanks for the information, though is there any way to keep the PHP from modifying the count file until the JS function is called?
08-28-2007, 05:16 AM
I had a script, that would run a php snippet when a function was excecuted and it worked.
document.write = '<?php DO W/E ?>';
THe only thing I see wrond with this, is that you would have to use php for the views, and mysql. Inside the <?php update mysql databse with +1 views, and do a link with the button like:
<a href=# onclick="update()"> Expand/Collapse </a>
08-28-2007, 05:21 AM
08-28-2007, 05:31 AM
...or is it also possible to write into the file as PHP lets you do via ECHO? Something like this?
...set something that will write or append to the page within the JS function when/if it is actually called by the user... somehow?
08-28-2007, 05:38 AM
It is true that the document.write() displays the content, but it is displaying content that was already present in its' final form. It did not cause any PHP code to be executed.
If I remember correctly, there was another recent thread, in the past few weeks, that addressed this same subject.
08-28-2007, 05:50 AM
This will help - http://www.w3schools.com/ajax/ajax_httprequest.asp
If you want to send a piece of information to or even just signal a web server to increment a count based on an event happening in a browser, and you don't want to refresh the whole page, you are going to need to use a XMLHttpRequest object.
08-28-2007, 06:23 AM
I tried the document.write and that either only prints the variable name to the screen or hangs the browser up.
08-28-2007, 06:52 AM
Outstanding I scraped this together and it seems to be working as desired, thx much.
// Firefox, Opera 8.0+, Safari
// Internet Explorer
alert("Sorry, Your browser does not support AJAX! Unable to Update Flower Count. Please visit www.GetFireFox.com");
08-29-2007, 02:49 AM
I was recently using a cool little vacation gallery that would set a cookie if person clicked on an image, and i did the onclick function, with a php set cookie option. It seemed to work awsomely.
document.write = '<?php $value="true"; setcookie("time","$value", time()+7200,"/"); ?>';
I know it worked, because when people would go to the page, without clicking the image, it would display a message saying take a look at the latest users. If it was clicked, it wouldn't show it.
08-29-2007, 06:41 AM
That's amazing! You found a way to circumvent the whole php being server-side problem. And the browser executed the php code too? Fantabulous.
08-29-2007, 07:13 AM
That's amazing too, because the document.write = part of that is syntactically incorrect and just produces an error on the page in the browser (tested.)
From the PHP manual for the setcookie() function -
Cookies will not become visible until the next loading of a page that the cookie should be visible for...
08-29-2007, 02:36 PM
I think I tried that, and it seemed that cookies weren't set. I don't have time now, but I will do it later on today to see if it actually works, or it was just my mistake (most likely since noone agrees with me).
08-29-2007, 03:15 PM
A quick test - if you have a web page with that code on it, view the source of the page in your browser and tell us what you see.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.