Jump to content

[PHP && JavaScript] load a javascript file


Jack McKalling

Recommended Posts

When I want to have some PHP variables ready to use for JavaScript, I have to script way round, don't I......I can echo PHP variables into a javascript block like this:

<script type="text/javascript">var SomeThing = '<?php echo $A_Variable ?>'</script>
Or for bigger situations like this:
<script type="text/javascript"><?phpecho "var SomeThing = '{$A_Variable['0']}'; var AnotherThing = '{$A_Variable['1']}'; \n";?></script>
Whatever hey :)But I don't want to do it like either these methods when the javascript you echo with php will be written through a loop, and therefor may be big.Example:
<script type="text/javascript"><?phpecho "SomeThing = [] \n"; //Global JavaScript variableforeach ($Array as $fieldName => $fieldContent){echo "SomeThing['$fieldName'] = '$fieldContent' \n";}?></script>
I choosed for an external javascript file.When php comes to echo the javascript block, I let it rewrite the files content with fopen(), fwrite() and fclose().The mayor disadvantage of this, is after the rewrite, the browser also has to know that it should refresh that file and not to get it from the cache.How Can I Do That :)Else I get after the rewriting the same file as before, and not the updated one....Now I have to hit the browsers refresh button every time I visit the page (and rewrote the external file's content) Edited by Dan The Prof
Link to comment
Share on other sites

Okay, here is the deal.I have a main document called "Adminform". It uses "phpEdit" for its forms actions. I have a database with data about category buttons on Adminform. Each button on the page sends the browser to the same Adminform, but with other forms active on it. One of the buttons sends to the form to edit the data that defines all those buttons (including himself).The phpEdit also sends back to Adminform, when it did all the requested actions. For each form it does different things, but only a few different send-through methods. For example: when the action returned an error, it does not send through at all, only show a button to re-input the data at the form.When at Adminform, the user has chosen the form for the category buttons editing, Adminform must use the data from the database. Becouse that form shows some filled fields with representing data for each button. JavaScript is the language that can handle the data inside the form, so it should get its data from the database. However, JavaScript is not able to do that by himself so that is why I wrote with PHP his external file (explained in above post). PHP writes the JavaScripts declaring of all the database data in variables.So when the page loads the button-edit-form, it also loads the external file to be able to use all data. And when the edit is submitted, phpEdit edits both the database and external file.Except, each time Adminform loads that form, it searches his cache for the external file instead of directly downloading it.This means the just updated file does not get implemented, the old one does.-----------I came up with a (still problems causing) sollution.When I use a redirecting META REFRESH at phpEdit before it goes back to Adminform, the external file does get directly downloaded.The problem is, I want phpEdit to offer a "I do not wish to wait"-and-skipping-the-timeout-button :)(which omits the meta refesh)

Link to comment
Share on other sites

I want to redirect to a page that contains an external file, and which must be refreshed before loaded :)META-REFRESH can, but how can I skip its timeout with a button called "I do not wish to wait"?By the way, it you're not an advanced programmer in PHP / JavaScript, you probable can't help me :)

Edited by Dan The Prof
Link to comment
Share on other sites

Thank you :)It is exactly what I needed, it said how to turn an external JavaScript file into a PHP file, which is forced directly downloaded from the server instead of first the cache :)Great help, you may do that more often :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...