...

View Full Version : general function help



PTReese84
08-23-2011, 10:38 PM
Hi,
I need help creating a function. It want it to: run when the page loads and every x seconds after. On loading I need it to connect to a db(preferably w ajax) and store results as variables I can reuse in other functions. And finally, after some math equations I can do, print the results on the page.

I've been toying with this and I've realized its not as easy as I thought. Think I'm having problems with variable scope. Php is my main scripting language, of which I am fluent and have a working version of this. But I want it to run on the client side and relieve my server.

Sorry if this is vague or incorrectly formatted, I'm on a phone browser.

A guide or detailed demonstrations would be great, however pointing me in the right direction would me most apreciated.

Old Pedant
08-23-2011, 11:02 PM
Unless you need to have overlapping results--that is, some of your prior results being used at the same time you are grabbing new results--just make *all* your variables page scope and you should be fine. That is, do *not* use var name inside of your functions. Declare the relevant variables outside the functions (or not at all) and they will be page scope.

But your question is so vague it's hard to guess more than that.

PTReese84
08-23-2011, 11:30 PM
I thought of that. Apparently I'm not calling them correctly.

Two specific problems I'm having are calling the variables in the main page and assigning them values from a database.

If a=1 inside my external js, what do I need to do to get that value to the index page. I've tried document.write(a) but I get a undefined. I though "a" would be global since my external js variable has no "var" when assigning.

When do a php echo in the head I have no problems assigning values to variables, but when I try it in ext the values aren't set. What's the proper code for this?

Old Pedant
08-23-2011, 11:41 PM
Sorry, you've lost me.

You need to show some code example(s), I think.

When you say "external js page" do you just mean one you included in the main page via

<script type="text/javascript" src="external.js"></script>

?? If so, then any variables defined in that file should be available in the main page at any point *after* the </script> tag for the external file.

Old Pedant
08-23-2011, 11:44 PM
Maybe I just go it.

Are you saying you are doing


<script type="text/javascript" src="external.js"></script>

And then expecting to be able to do something like

var a = "<?php echo something; ?>";

inside of the "external.js" file????

Nope. No way. Never.

If you want PHP in the external file, then the external file must *ITSELF* be a PHP file:


<script type="text/javascript" src="external.php"></script>

Now, that file could then actually consist of 98% JavaScript with just the needed PHP, but if, for example, the main page is using a database to pull data from, then the external.php file would also have to do so.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum