Forum Moderators: open

Message Too Old, No Replies

include php through javascript

         

cameraguy

11:52 pm on Dec 3, 2004 (gmt 0)

10+ Year Member



Hi

I need help with the following problem.

I would like users to be able to call on a php script through a javascript command like so:

<script language="javascript" src="http://file.php"></script>

But it does not work ie. nothing shows on the screen.

Is the above command correct?

How do I need to alter my php file in order to show on the users'onw website? Much previous reading points to using document.wite()... but how, where?

Thank you for your help.

cameraguy

1:11 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



I know this topic may have been discussed before... Just point me in the right direction please.

Here is what I have done:

index.php
<script type="text/javascript" src="http://...../hello.php"> </script>

hello.php
<? echo"document.write('remote hello');";?>

When I execute hello.php I get: document.write('remote hello');

But when I execute index.php, I get nothing! I guess I am expecting that src="http://roberto.speich.free.fr/NeoCOUNTER/hello.php"> be replaced by document.write('remote hello');

What do you think I am doing wrong?

Bernard Marx

2:36 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you think I am doing wrong?

You are trying to execute a server-side script on the client.

[blue]<script src="blah.php" type="text/javascript"></script>[/blue]

This will use the output of the php as an external script. This has uses, but it won't do what you want.

What you are trying to do needs to be done on the server.

RonPK

4:50 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cameraguy, if you want PHP to output JavaScript, you'll need to send the correct MIME-type. PHP output defaults to text/html, whereas you need application/x-javascript.

<? 
header("Content-type: application/x-javascript");
echo ...

I hope this is what you meant. Ohterwise, ignore this post and do what Bernard tells you to ;)

Welcome to WebmasterWorld, by the way.

Bernard Marx

9:07 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On re-reading, I think that's it.

cameraguy

10:22 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



Thank you for your help. I am going to try this right away and let you know.

cameraguy

11:53 am on Dec 8, 2004 (gmt 0)

10+ Year Member



I am affraid I still have not found the solution.

Here is the script that's causing me trouble. Perhpas you can spot the error...

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script language="javascript" src="http://.../index.php?user=66175"></script>
</body>
</html>

When I type 'index.php?user=66175' directly in my browser I am able to $_GRT the value in index.php, but when I execute the above script in a file called text.html, nothing happens. I do however know that the script takes me to index.php, but apparently not the variable. What am I doing wrong?

PS. I realize that I have posted a similar message in the PHP forum...

RonPK

1:25 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, let's continue over there [webmasterworld.com].