SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to

    Run a function when a link is clicked.

    For example i have a add on my page and when the link is clicked it will pop up in a new window (_blank)

    Id like to keep track of how many times its clicked. So i want a basic funtion to run each time its click. Is this at all possible?

  2. #2
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    You could do this in a few different ways. The first being
    create a php file say 'clicks.php' and in this file
    ad your function and some code like so:

    PHP Code:
    // Lets check for the click
    if($todo == "go"){click_func};

    // Here is the function
    function click_func($id){


    Then in the URL use this: http://www.yourdomain.com/clicks.php?todo=go&id=1

    Link would be as follows:
    Code:
    <a href="http://www.yourdomain.com/clicks.php?todo=go&id=1" target="_blank">click here</a>
    Basically you are getting the url to send them to using
    the $id variable we set in the URL do your updating
    and send the user to the new site.

    Hope this helps.
    Ray
    http://cwdhost.com/
    Plans from $9.50/mo.
    Reseller Plans from: $35/mo. - Host Unlimited Domains

  3. #3
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    That works

    I thought i could do something like in javascript onclick() but i guess not.

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SUre you can.

    This is a two part process.

    First make your javascript to open the new window.

    Then make a page php page to increment your counter and have your form submit to that.

    So your form is going to be like this:

    Code:
    <form action = "script.php" method = "post" onSubmit = "javascript">
    
    ...
    ...
    </form>
    USe OnSubmit instead of onclick because some people submit forms by pressing enter instead of clicking the submit button.

    Then here is the tricky part. Your script.php has to return no response to the browser. To do this you add this line of code to it's last line:

    PHP Code:
    header("HTTP/1.0 204 No Content"); 
    This is what is also called a "No Refresh Link" because when you access this page your current page will not change.

    So when someone submits your form the information will be submitted to your php script but the browser will not load the php script. Instead the browser will load whatever you have in your javascript.

    Hope this all makes sense, it's probably a very round-about explanation.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, that's really neat aspen!
    Paul Davey
    webmaster for Whitford Church of Christ


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
  •