SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Feb 2005
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php or javascript solution

    if i have a link say

    <a href="http://www.google.com">google</a>

    and i want to register the clicks to that link


    use something like linking to redirectexit.php?siteid=20 (php approach)

    or use an onclick on the link to trigger javascript to enter it into the database.
    does this approach work? and where can i find more about it?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you elaborate more what do you intend to do, are you try to trigger your backend script to work or just to obtain data from your script?

  3. #3
    SitePoint Addict
    Join Date
    Feb 2005
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just keeping a count of the clicks

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You will need a servier side script to do that - JavaScript could count the clicks for the current page (0 or 1) or use a cookie to keep count by visitor but in either case you wont get to see the count because it never gets passed from their computer.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    Feb 2005
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry for being so unclear

    i meant using ajax to call the php code to insert it in the database.
    thing is im not sure what happens when someone clicks a link and moves of the page. will the javascript still be able to successfully call the ajax function?

  6. #6
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would still use php in this case. Javascript can be turned off by the end user or they could be using a browser for which your script will not function.

    Php is working in a stable envrironment and will work regardless of what the end user is doing/using.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I imaging you are talking about something like:

    <a href="http://www.google.com" onclick="submitClick()">google</a>

    where submitClick() would use ajax to send the click to the user.

    I am not 100% sure what will happen, I have not tested it, but you may have to worry whether the server will always get this request because since you will be moving to a new page the code making the ajax connection will be removed probably before the request can finish.

    You could set the request to be synchronous in order to avoid that but that would make the L&F of this link unnatural to the user because it will freeze the browser window for a sec before appearing to accept the click, much worse if your server is busy at the time, it might appear as though the browser crashed. I also think you are going to get an amount of stickiness even for the asynchronous request.

    So, again, better go with a server side solution.

  8. #8
    SitePoint Addict
    Join Date
    Feb 2005
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes but that would make my link point at something else than what it is going to.
    are there any complications regarding SEO?


Bookmarks

Posting Permissions

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