...

View Full Version : include() and require()



serakul
08-28-2004, 09:16 PM
Are there any javascript functions equal to the php include() and require()?!?

Or how can I include files?!?

liorean
08-28-2004, 10:12 PM
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.

serakul
08-28-2004, 10:50 PM
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?!?

joh6nn
08-29-2004, 02:17 AM
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.

jamescover
08-29-2004, 02:57 AM
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

serakul
08-29-2004, 09:38 AM
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.

jamescover
08-29-2004, 03:48 PM
<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

serakul
08-30-2004, 09:43 AM
<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!?

fci
08-30-2004, 10:58 PM
<SCRIPT>
function Spiro()
{
</SCRIPT>
<?php include 'http://blabla/Some.php'; ?>
<SCRIPT>
}
</SCRIPT>


that is what I would do

joh6nn
08-31-2004, 03:24 AM
<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>

serakul
08-31-2004, 08:44 AM
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.

jamescover
08-31-2004, 09:13 AM
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

joh6nn
08-31-2004, 07:05 PM
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?

AaronW
08-31-2004, 07:44 PM
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:


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

Then:


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

serakul
08-31-2004, 10:37 PM
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?!

AaronW
08-31-2004, 10:53 PM
See my post... Right before your last one...

Use:

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

Where phpscript.php is a script that deletes the row from the database.

As for keeping it clean of users who don't have JavaScript support, just use a cron job (google for 'cron jobs') and run cleanup.php (a script to remove rows that are, say, 10 minutes old) every 5 minutes or so.

However, going to these lengths to get a count of who's online is overkill I think. People won't see the exact number of who's online unless they keep reloading the page every second, so you're fairly safe to just insert them into the database and assume they're on the site for, say, 10 minutes. If they reload the page, their timer gets reset to 10:00 and starts ticking down again from there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum