using <Script src=""> to call a php file lea
Moderator: General Moderators
using <Script src=""> to call a php file lea
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.
I should also note that when I use the same code and simply use an include it displays just fine.
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.
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.
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
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:
here is the html page:
here is the php script:
Code: Select all
<?php
header("Content-Type: text/javascript");
print("<div>THIS IS A TEST</div>");
?>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>It is obvious that this is not JavaScript, but HTML ...lapith wrote:Code: Select all
<?php header("Content-Type: text/javascript"); print("<div>THIS IS A TEST</div>"); ?>
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
would a valid solution to this possibly be to use
Or would this cause problems on the end user's site?
Code: Select all
print("document.write('<div> this is a test</div>')");