Hi! as we can use a javascript using <script src="xyz.js"></script> tag in .htm file. can we use such a script for a php file? infect i want to use a php counter on htm page but want to avoid <iframe> tag? in the php file after some commands one variable is displayed like echo $count; i've tried using <script src="http://domin.ext/counter.php"></script> but its not showing the variable value though counter works in this way i.e. counter is incremented but not displayed on .htm page; I have also the tried script tag for a php file having a single echo statement for variable as well for string; also i have tried image on php file bt no display; please help me out if it is possible to display php file in this way...
You cannot include a php file from within a script tag. PHP is a server side scripting language, and the <script> tag denotes a client side script. <iframe> will essentially include an external file so that's really your only option I would think -- aside from parsing all of your htm files as php and doing something like <? require_once('/path/to/counter.php' ?> You can parse htm files as php from within a .htaccess file. Syntax may be off, but it's something like: ForceType application/php .htm You can google around or search these forums for the exact syntax. There are a lot of references.
You can include a php file using javascript src tag. You have to take care of header tag inside the php file. I have developed on counter script which works in any html page. Through JavaScript the php codes get execuated. This is the code. If you use this you will get a counter at any html or any other page. <script language="javascript" src="http://www.plus2net.com/counter/count.php?id=3150311751"> </script> The header of the count.php files says Header("content-type: application/x-javascript"); Try with this .... HTH
Actually you can. Here is the code from the adsense tracker I use on my site: <script language="javascript" src="/astracker/ast.php"></script> Code (markup): It's the action of requesting the script that makes it execute server side - if you return javascript then that code will execute in the browser. Call the PHP file in your browser and ensure it is working, then throw it into the javascript request and use "Live HTTP Headers" plugin for firefox to ensure it is being requested correctly and you should be good to go... Now that I think of it, the NicheBot Adsense Tracker also uses that trick.... Hope that helps. Steve. PS. the ForceType trick is also cool, just put the htaccess file in a directory and drop a a php file in there, but make the extension .js - it will return like a js file but be parsed via PHP
Now that I am re-reading your first post - I see I did not quite answer the question... If your PHP code is executing fine and doing the increment as expected, the problem is that you are not returning javascript for the browser to render... try returning the count in a properly formatted document.write statement
Still facing the same problem. Now i am testing it such that i have counter.php placed in C:\apache\htdocs\test\counter.php and content of this file is: <?php header("content-type: application/x-javascript"); ?> <?php echo '<script type="text/javascript">'; echo "var count = 32;"; echo "document.write (count); "; echo "</script>"; ?> PHP: i have also tried the following code as well <?php header("content-type: application/x-javascript"); ?> <?php $count = 100; echo $count; ?> PHP: and in the test.htm file placed on my desktop i am using: <script language="javascript" src="http://localhost/test/counter.php"></script> but the values are not displayed in htm file. And when the counter.php is run just by typing http://localhost/test/counter.php following error is produced: Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\test\counter.php:2) in c:\apache\htdocs\test\counter.php on line 3 Again when trying this with actual counter code file counter is working properly but not displayed in htm file; Please suggest any solution;
Headers have to be the first thing sent. Strangely it looks like your headers are the first line. I'd check the file again to make sure.
Try the following, as you are already telling the browser what you are executing is Javascript you do not need <script> tags in your PHP. The <script src="/myscript.php" type="text/javascript"></script> tells the browser that whatever code is in myscript.php is valid javascript. <?php header("content-type: application/x-javascript"); echo "var count = 32;"; echo "document.write (count); "; ?> PHP: [edit: I also have done this many many times without the need to include a header. Simply output the javascript code]