I have both php and javascript in my code. There are links which change javascript variables which are then passed to php via $get. To change the values I'm pretty sure I would have to refresh the page. Where could I put default values such that they load the first time the page does but not on every successive refresh as they would overwrite the values I have already? Thank you!
A:
If cookies are allowable, you could set a cookie the first time you load the page. Then, every subsequent page load, your js function checks for the cookie.
<div id='name'></div>
<script type="text/javascript">
function setDefaults() {
//SET PAGE DEFAULTS - ONLY RUN AT INITIAL PAGE LOAD
var nameDefault = "djronde";
document.getElementById('name') = nameDefault;
document.cookie = "page_loaded=1";
}
if (document.cookie.indexOf("page_loaded") != -1) {
setDefaults;
}
</script>
Byron Sommardahl
2010-07-14 15:50:46
A:
Instead of having the default values 'hard wired' into the javascript section of your page, why not embed php fragments into the javascript, so that the values are generated dynamically from values stored on the server side. When you pass the updated javascript values to the php, save them somewhere so that they can be used the next time the page is generated.
I'm not a php developer, so I don't know the syntax off the top of my head, but here goes...
<?php $serverVariable = resultOfSomeOperation();?>
.
.
.
<script language="javascript">
var clientVariable = <?php echo $serverVariable;?>
.
.
.
</script>
I hope you get the general idea.
belugabob
2010-07-14 15:58:28