...

View Full Version : Execute php page from javascript



the-dream
01-13-2008, 09:10 PM
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

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!

Thanks a million!
~ Christian

mlseim
01-13-2008, 10:21 PM
Did you try doing this yet on your standard .html pages?

<script type="text/javascript" src="http://www.mysite.com/track.php"></script>

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

// all of your PHP scripting goes here.

// the Javascripting is expecting something back,
// so give it something, like a blank space.
$string="&nbsp;";
Header("content-type: application/x-javascript");
echo "document.write(\"$string\")";

?>

the-dream
01-14-2008, 10:04 PM
Perfect, Thank You!

the-dream
01-16-2008, 09:32 PM
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!

StupidRalph
01-16-2008, 09:49 PM
You should've glanced over the forum a little, someone just posted something with a subject title very similar...

http://www.codingforums.com/showthread.php?t=131359

mlseim
01-17-2008, 01:06 AM
I wouldn't think it matters about 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.

the-dream
01-17-2008, 07:03 PM
I wouldn't think it matters about 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 &nbsp; look this is the tracker http://www.thecentresecurity.com/DreamStats/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.


Is the Javascript Header and echo the last thing it does?

Yes, and there is no other echos on the page, and no whitespace!

mlseim
01-18-2008, 02:32 PM
hmmm .... sort of stumped.

For a test, I made a script that pulls a random "bumper-sticker" quote
out of a file and sends it back as Javascript.

Put this on a test page of yours and see what happens:

<script type="text/javascript" src="http://www.catpin.com/bumper_sticker3.php"></script>

Wherever that line is, when you view your web page, it should have a random quote.

It's using the same code that I had above (in post #2).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum