#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    Question sending javascript var to Php?


    Hi there, I hope someone can help me...

    I can't figure out how to do this?

    I have a page on a host that doesn't allow php scripts. I would like to use javascript to get the last page my user was on. I dunno how to send a query string to a php page that'll take the info and insert it into a mysql databse...

    I have seen a counter that uses CGI and uses the img src to send the data,

    <script type="text/javascript" language="JavaScript">
    <!--
    var reF=document.referrer;

    document.write('<a href="http://www.counter.com/index.cgi?refID=yahoo" target="_blank"><img src=');
    document.write('"http://www.counter.com/cgi-bin/stats/image.cgi?');
    document.write('user=yahoo&page=www.yahoo.co.uk&bgcolour=black&digits=5&fontcolour=white&total=no&re F='+reF+'"');
    document.write('border=0></a>');
    // -->
    </script>


    Is this possible with php?
    I don't want a link that the user has to physically click to send the query to php, must be automatic.


    Thanks in advance for any help
    Rob
  2. #2
  3. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    The only method available to use is to have variables in the URL of the page... javascript accesses these natively by refering to the var, PHP accesses URL variables through $_GET['var'].
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    so it''s not possible?


    I am new to both javascript and php so go easy on me :P


    This is what I have worked out from another message posted on the forum...

    <<script type="text/javascript" language="JavaScript">
    <!--

    var reF=document.referrer;
    var url;

    url = "http://myphpwebsite/fscount.php?var=" + reF;

    // -->
    </script>

    Now that doesn't send it only makes a url of it. Is there any way of sending this url automatically?

    Here is my fscount.php:

    $query = "INSERT INTO fscount (
    referrer)

    VALUES (
    '$reF')
    ";

    $result = mysql_query($query)
    or DIE ("Couldn't Execute query");

    ****I don't want the user to be taken to this php page*****
    So the information will be entered straight into mysql

    Any ideas for that,

    Thanks Rob
    Last edited by denz_99; August 15th, 2003 at 02:43 PM.
  6. #4
  7. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    PHP isn't going to know what reF is, since this is part of the javascript code, not the URL.

    PHP is going to see the 'var' variable, which is in the URL.

    You would access 'var' in PHP by saying:

    PHP Code:
    $my_variable $_GET['var']; 
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    A possible solution?


    Just been thinking & thinking what I could do, and then it came to me!

    I could make the page have two frames one with the php file and the normal html file. That should work shouldn't it?

    Thanks for your help gonna try that solution out.
    Rob
  10. #6
  11. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    Frames have their own set of issues, but if that's an acceptable tradeoff for you, then it should be workable. For the record, I strongly advise not to use frames in publically accessible websites.
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    any other similar way?


    What about a hidden input textbox...but is it able to post automatically to the php page?
  14. #8
  15. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    PHP can detect the last page your user was on through $_SERVER['HTTP-REFERER']; ... not sure if you were aware of that. You don't *need* javascript to do that for you.
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    no can't do that


    this page doesn't reside in a php enabled site....this page will go to a php site but i would like to get the info from where they came from you see
  18. #10
  19. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265

    Re: no can't do that


    Originally posted by denz_99
    this page doesn't reside in a php enabled site....this page will go to a php site but i would like to get the info from where they came from you see
    Oh, I see - you want the page two pages previous to the one that parses PHP.

    OK.

    So, why not just include the http-referer page in the URL using javascript, and have PHP pick it up from the URL?
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    13
    Rep Power
    0

    solved it!!!


    <script language="JavaScript">
    var image = "http://www.mysite.nonphphost.co.uk/pixel.gif";

    var referrer = escape(document.referrer);



    if (navigator.javaEnabled() == 1){ var javaa = "Enabled";} else {var javaa = "Disabled";}
    var link = "<img src='http://phphost/referrer.php?referrer=" + referrer + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";
    document.write(link);
    </script>


    It works!

    Rob

IMN logo majestic logo threadwatch logo seochat tools logo