Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    general function help

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Maybe I just go it.

    Are you saying you are doing
    Code:
    <script type="text/javascript" src="external.js"></script>
    And then expecting to be able to do something like
    Code:
    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:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •