Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2004, 08:16 PM   PM User | #1
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
include() and require()

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

Or how can I include files?!?
serakul is offline   Reply With Quote
Old 08-28-2004, 09:12 PM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
liorean is offline   Reply With Quote
Old 08-28-2004, 09:50 PM   PM User | #3
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
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?!?
serakul is offline   Reply With Quote
Old 08-29-2004, 01:17 AM   PM User | #4
joh6nn
wei wu wei


 
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
joh6nn is an unknown quantity at this point
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.
joh6nn is offline   Reply With Quote
Old 08-29-2004, 01:57 AM   PM User | #5
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
Quote:
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. "
jamescover is offline   Reply With Quote
Old 08-29-2004, 08:38 AM   PM User | #6
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
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 09:13 AM..
serakul is offline   Reply With Quote
Old 08-29-2004, 02:48 PM   PM User | #7
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
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. "
jamescover is offline   Reply With Quote
Old 08-30-2004, 08:43 AM   PM User | #8
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
<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!?
serakul is offline   Reply With Quote
Old 08-30-2004, 09:58 PM   PM User | #9
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
Code:
<SCRIPT>
function Spiro()
{
</SCRIPT>
<?php include 'http://blabla/Some.php'; ?>
<SCRIPT>
}
</SCRIPT>
that is what I would do
fci is offline   Reply With Quote
Old 08-31-2004, 02:24 AM   PM User | #10
joh6nn
wei wu wei


 
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
joh6nn is an unknown quantity at this point
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.
joh6nn is offline   Reply With Quote
Old 08-31-2004, 07:44 AM   PM User | #11
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
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.
serakul is offline   Reply With Quote
Old 08-31-2004, 08:13 AM   PM User | #12
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
That will load the script into the browser cache.

Quote:
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
__________________
"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. "

Last edited by jamescover; 08-31-2004 at 08:52 AM..
jamescover is offline   Reply With Quote
Old 08-31-2004, 06:05 PM   PM User | #13
joh6nn
wei wu wei


 
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
joh6nn is an unknown quantity at this point
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.
joh6nn is offline   Reply With Quote
Old 08-31-2004, 06:44 PM   PM User | #14
AaronW
Senior Coder

 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 1,223
Thanks: 0
Thanked 0 Times in 0 Posts
AaronW is an unknown quantity at this point
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');">
__________________
offtone.com | offtonedesign.com
AaronW is offline   Reply With Quote
Old 08-31-2004, 09:37 PM   PM User | #15
serakul
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
serakul is an unknown quantity at this point
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?!
serakul is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.