Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-13-2008, 08:10 PM   PM User | #1
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
Execute php page from javascript

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!

Thanks a million!
~ Christian
__________________
Branchr Advertising Network
the-dream is offline   Reply With Quote
Old 01-13-2008, 09:21 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 Code:
<?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\")";

?>

Last edited by mlseim; 01-13-2008 at 09:27 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
the-dream (01-14-2008)
Old 01-14-2008, 09:04 PM   PM User | #3
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
Perfect, Thank You!
__________________
Branchr Advertising Network
the-dream is offline   Reply With Quote
Old 01-16-2008, 08:32 PM   PM User | #4
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
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!
__________________
Branchr Advertising Network
the-dream is offline   Reply With Quote
Old 01-16-2008, 08:49 PM   PM User | #5
StupidRalph
Senior Coder

 
Join Date: Mar 2003
Location: Atlanta
Posts: 1,037
Thanks: 14
Thanked 30 Times in 28 Posts
StupidRalph is on a distinguished road
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
__________________
Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.
StupidRalph is offline   Reply With Quote
Old 01-17-2008, 12:06 AM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 01-17-2008, 06:03 PM   PM User | #7
the-dream
Regular Coder

 
the-dream's Avatar
 
Join Date: Mar 2007
Location: Northamptonshire, UK
Posts: 477
Thanks: 8
Thanked 4 Times in 4 Posts
the-dream can only hope to improve
Quote:
Originally Posted by mlseim View Post
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/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 View Post
Is the Javascript Header and echo the last thing it does?
Yes, and there is no other echos on the page, and no whitespace!
__________________
Branchr Advertising Network

Last edited by the-dream; 01-17-2008 at 06:05 PM.. Reason: Missed a bit! ;)
the-dream is offline   Reply With Quote
Old 01-18-2008, 01:32 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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).
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 AM.


Advertisement
Log in to turn off these ads.