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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    include() and require()

    Are there any javascript functions equal to the php include() and require()?!?

    Or how can I include files?!?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    JavaScript has no built in equivalents of those. Traditionally you use the script tag in the HTML page, so why would Netscape have added them?

    If you want to read a file from server, though, you can do that using browser extentions. Have a look in the JavaScript FAQ.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well...The entire problem:

    I need a js function wich to call some php code inside. The code is some actions with a mysql db.

    It have to be something like this:

    <SCRIPT>
    function Sp()
    {
    <?php FUNCTION; ?>
    }
    </SCRIPT>

    But the php code doesn't submit to be inside a java function. The code is simply activated just like it would be if it wasn't in a function at all...

    So when I couldn't mix the two languages I started to look for a way to make the function in one only.

    The two options are : to use some java function for db connectivity (there isn't such) or to replace the php code with some java equal.

    I can't do the last but I found out that if I push that function in an outside file and I call it somehow with some java function it will work.

    I already did it using the open method. But it opens the scipt in a new window.

    So is there any way that problem to be done?!?

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    you can call an external, server-side script by creating a new image object, like so:

    var serverSide = new Image();
    serverSide.src = "http://www.whereever.com/phpStuffs/widget.php";

    alternatively, instead of trying to put the PHP inside of JavaScript, you could try putting the JavaScript inside of PHP.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    php include() and require()?!?
    The equivalent would be:

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

    and

    <script type="text/javascript">document.write("something");</script>

    The problem is that php can't do anything after the markup has already reached the client--it has to be parsed beforehand.


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #6
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by joh6nn
    you can call an external, server-side script by creating a new image object, like so:

    var serverSide = new Image();
    serverSide.src = "http://www.whereever.com/phpStuffs/widget.php";

    alternatively, instead of trying to put the PHP inside of JavaScript, you could try putting the JavaScript inside of PHP.
    Sure I could...but how can I set a php function to the onUnload event?!?

    Is this serverSide object all that have to be done?!
    I mean this do not work:

    <SCRIPT>
    function Sp()
    {
    var serverSide = new Image();
    serverSide.src = "http://www.whereever.com/phpStuffs/widget.php";
    }
    </SCRIPT>

    Do I have to add anything ese?!


    About <SCRIPT SRC...> it may work.

    <SCRIPT>
    function Sp()
    {
    <SCRIPT SRC='http://blbl'></SCRIPT>
    }
    </SCRIPT>

    Is this what I need?! The status bar says : Done, but with errors.
    Last edited by serakul; 08-29-2004 at 10:13 AM.

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <SCRIPT>
    function Sp()
    {
    <?php FUNCTION; ?>
    }
    </SCRIPT>




    If you use:

    <?php include ('somefile.php') ?>

    the script will be parsed for that page, before it reaches the client.

    On the otherhand, if you use:

    <script type="text/javascript">document.write("<?php include ('somefile.php') ?>");</script>

    the include will not be executed, but merely written to the page.

    I don't know, if that answers your question, but if you explain (explicitly) what you are trying to accomplish, maybe, someone can help you with the functionality.


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #8
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <SCRIPT>
    function Spiro()
    {
    </SCRIPT>
    <Script SRC=http://blabla/Some.php></Script>
    <SCRIPT>
    }
    </SCRIPT>


    Is this suposed to work!??I need something like that-a function that include a php script.
    Open() do not do the job. Can anyone tell me how to fix that!?

  • #9
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <SCRIPT>
    function Spiro()
    {
    </SCRIPT>
    <?php include 'http://blabla/Some.php'; ?>
    <SCRIPT>
    }
    </SCRIPT>
    that is what I would do

  • #10
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by fci
    Code:
    <SCRIPT>
    function Spiro()
    {
    </SCRIPT>
    <?php include 'http://blabla/Some.php'; ?>
    <SCRIPT>
    }
    </SCRIPT>
    that is what I would do
    i don't know that i've ever tried something like that, but i'm about 98% sure script tags don't work that way. the following variant of that idea should work, however:

    <SCRIPT>
    function Spiro() {
    var phpScript = new Image();
    phpScript.src = 'http://blabla/Some.php';
    }
    </SCRIPT>
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #11
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welll...Fci, you can't do that. .)
    It's not only that the php code will be executed just like it is not inside a function...The first </SCRIPT> is closing the first <SCRIPT> and the function is not finished.


    Johshn, I tryed...but still no...doesn't work.

    I've put all this in here:
    http://bgrulers.hosters.uni.cc/Scr.htm. Just check out the source.

    You may open the file that should be open to see that the path is correct.

  • #12
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That will load the script into the browser cache.

    If you use:

    <?php include ('somefile.php') ?>

    the script will be parsed for that page, before it reaches the client.

    On the otherhand, if you use:

    <script type="text/javascript">document.write("<?php include ('somefile.php') ?>");</script>

    the include will not be executed, but merely written to the page.

    I don't know, if that answers your question, but if you explain (explicitly) what you are trying to accomplish, maybe, someone can help you with the functionality.
    I think, if you will better explain what you are trying to do, someone will be able to help you.

    First thing you need to do is to give your pages a php extension, for any php script to work on the page.



    -james
    Last edited by jamescover; 08-31-2004 at 09:52 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #13
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i agree with James; we need more information. what is it that the php you're trying to call is supposed to be doing? if you're just trying to execute an arbitrary php function when a browser event fires, the method i've shown you is fine. or are you trying to get the php to print some output to the browser?
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #14
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't call a PHP function with javascript... the PHP won't even exist in memory by the time the browser sees the <script> tag.

    To elaborate on what's already been said, try this:

    Code:
    <script type="text/javascript">
    function execPHP (path)
    {
      var php = new Image();
      php.src = path;
    }
    </script>
    Then:

    Code:
    <body onunload="execPHP ('http://www.yoursite.com/phpscript.php');">

  • #15
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure...Here is it...the problem...in details.

    Well...It's about a script that calculates the users online. I found some dozens smilar scripts with diffrent ideas, but all of them had many disadvantages and first of all they did not show the exact number of users, but the hits of the site in the last hour or some similar relativity.

    I would like when a user close the site that to be recorded at once. Soo...how else but with the onunload event?!?

    When I started to realise the idea I didn't know a single function on JavaScript()...besides I already made a users online script...not that good as I would like, but good...too.

    Soo...Here's the idea:

    A mysql table:
    Column 1: IP varchar(15)
    Column 2: bigint LastAccessTime

    1.The user open the page. The user's IP is recorded.
    2.The user close the page and the IP should be removed from the database.
    3.The user desides to refresh the page...well the onunload event is fiered and the IP is removed, but with the refreshing of the page the IP is added again.

    Now what's that AccessTime you would say...
    Well...what if the user just shuts its PC and there's no time for decrement...or he loose connection to the internet or whatever...
    With every add the enough old records are deleted...That makes the script relativity again...but not that much...besides at every refreshing or reloading of the home page the user's IP will be added again...

    And that would be all the problems...but no-just one more-the JavaScript.

    On every fireing of the onunload event should be executed that php code-the deleteing of the user's IP. That's what I nead that functin for.

    Onather problem is that not all the brouzers supporst JavaScript. Soo...if the increment fragment is in the same users online script it will be no problem to be executed, but the included with JavaScript will. So...that's easy...We simply put the increment fragment in a second file that is called with <SCRIPT SRC=...




    Well...That would be the entire problem...More details?!??

    The including of the file with that function is not the only desicion however.
    We can achieve the same using JavaScript connection to the db createing a new database object...I found that out recently. But I do not prefer that way...my db username and password will be reveald. Besides I can not make it do it...I can not make the connection.





    Any answers will be more than...hah...I already missed the dead line and I made that not that perffect users online script...Well...It's not something who knows how much important...But I started it and didn't like not to finishe it.


    Now...Any ideas?!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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