Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • Users who have thanked mlseim for this post:

    the-dream (01-14-2008)

  • #3
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Perfect, Thank You!

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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!

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    You should've glanced over the forum a little, someone just posted something with a subject title very similar...

    calling php from Javascript ---Problem Solved
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #7
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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!
    Last edited by the-dream; 01-17-2008 at 06:05 PM. Reason: Missed a bit! ;)

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •