Hey Guys,
First off I didn't quite know if this belonged in the JavaScript Forum or the PHP one so please move it if it is wrong!
I am creating a visitor counter and i want to know how I can execute a page of PHP from JavaScript.
Here is the PHP page:
PHP Code:
<?php
require_once('../Connections/dreamstats_db.php');
$colname_RSTrack = "-1";
if (isset($_GET['id'])) {
$colname_RSTrack = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_dreamstats_db, $dreamstats_db);
$query_RSTrack = sprintf("SELECT * FROM pages WHERE id = %s", $colname_RSTrack);
$RSTrack = mysql_query($query_RSTrack, $dreamstats_db) or die(mysql_error());
$row_RSTrack = mysql_fetch_assoc($RSTrack);
$totalRows_RSTrack = mysql_num_rows($RSTrack);
$id = $_GET['id'];
$visit = $row_RSTrack['visits'] + 1;
$result = mysql_query("UPDATE pages SET visits='$visit' WHERE id='$id'");
mysql_free_result($RSTrack);
?>
I want to execute that (track.php) from some JavaScript. It needs to be able to do on multiple domains. For example, track.php is on domain.com and I want to put the JavaScript to execute the code on example.com! These pages won't be .php they will most likely be .html/.htm! I really have no idea how to do this, hoping someone out there knows how!
You can execute any PHP script you want ... the key is to return
something back within a Javascript Header, even if it's just a blank space:
PHP Code:
<?php
// all of your PHP scripting goes here.
// the Javascripting is expecting something back,
// so give it something, like a blank space.
$string=" ";
Header("content-type: application/x-javascript");
echo "document.write(\"$string\")";
Eeek! Spoke too soon, I have had a lot of people complain that it's not working (That's not your fault!). And I think I know why, If on track.php I had an include, would it still work? Even if the JavaScript file is on a different domain to the include!
How does "track.php" exit?
Is the Javascript Header and echo the last thing it does?
And the fact that you say "many complaints" ...
Do you mean it works for some people, but not others?
That might be a good clue.
It dosent work for anyone. If you use the javascript however if you navigate to the file track.php on the server it works fine, you get the javascript look this is the tracker http://www.thecentresecurity.com/Dre...track.php?id=3
The PHP code connects to a database, would that matter?
EDIT: Oh and i forgot to mention, when you go to it directly it executes the PHP and adds the record to the Data Base.
Quote:
Originally Posted by mlseim
Is the Javascript Header and echo the last thing it does?
Yes, and there is no other echos on the page, and no whitespace!