using <Script src=""> to call a php file lea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

using <Script src=""> to call a php file lea

Post by lapith »

I am currently trying to call a php script from a page that may not be parsed by php. From the research I have done online I have found that calling it usng <script type="text/javascript" src=myphpscript.php></script> is the desired method. Having done this I now get an error stating that I am missing } in XML expression. This seems odd to me because I am not using XML at all. Is it possible that I am doing something that would be interpreted as XML?

I should also note that when I use the same code and simply use an include it displays just fine.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you using header() to send a proper content type for the response of "text/javascript"? It may be beneficial to post the script.
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

Post by lapith »

sorry about the delayed response. I got wrapped up in another task yesterday.

I am not sure exactly how header() will work. Let me explain my situation a little better.

I am working on a PHP script (essentially it is a widget) that I would like to allow people to copy one line of code into their website and display the widget. This means that I cannot assume that the person's site will be on a server that has PHP installed (otherwise a simple include would work just fine). From what I understand this is most commonly done using a call as the php script as a src tag. I have something like this:

<script type="text/javascript" src="widget_display.php"></script>

Widget display simply puts together a string that includes standard html tags (essentially it is printing out <div>all my html here</div>). I also am including a <style> tag and a <script> tag for some javascript that is used in my widget. So this string is put together and I simply use:

print($string);

The results of this have varied a bit. Sometimes I get that XML error (as stated above) other times I notice (using firefly) that widget_display.php has sent a response that is correct (all the HTML seems to be intact) however nothing is displayed in the browser.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You have to send headers (before any other output) that will inform the browser about the type of the content it is going to receive. It is done, as feyd suggested, by using header() function. The specific header you need in your case is "Content-Type: text/javascript" .
There are 10 types of people in this world, those who understand binary and those who don't
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

Post by lapith »

Thank you for your suggestions. Unfortunately the problem still exists. I have created a very short version of my code that should give you a better idea of what I am doing. This code also is sent as a response, but is not actually displayed in the browser.

here is the php script:

Code: Select all

<?php
header("Content-Type: text/javascript");

print("<div>THIS IS A TEST</div>");
?>
here is the html page:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<script type="text/javascript" src="http://192.168.1.12/tools/scripts/test.php"></script>
</body>
</html>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

lapith wrote:

Code: Select all

<?php
header("Content-Type: text/javascript");

print("<div>THIS IS A TEST</div>");
?>
It is obvious that this is not JavaScript, but HTML ...

Code: Select all

<?php
header("Content-Type: text/javascript");

print("alert('This is JS code');");
?>
There are 10 types of people in this world, those who understand binary and those who don't
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

Post by lapith »

would a valid solution to this possibly be to use

Code: Select all

print("document.write('<div> this is a test</div>')");
Or would this cause problems on the end user's site?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You can put whatever JS you want ...
The potential problems are the same as with ordinary embedded JS.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply