Run PHP function from HREF link?

Posted on 2006-06-06
Last Modified: 2008-02-01

I have a page with an <A HREF ...> link on it. When the user clicks that link I want to execute a PHP function (which updates a database table), but I don't want to leave the page I am currently on ... how can I achieve this?


Question by:themitch2004
    LVL 33

    Accepted Solution

    Use target="_blank" to set the link to open a new window, which calls the PHP page in question, or use AJAX, or submit a form on an hidden iframe.

    Author Comment

    Hi Snoyes

    I like the idea of the new window, how can I resize the window that is opened from this link? Is this treated as a 'popup' (ie will popup blockers prevent the window opening)? What if the user has javascript disabled?

    Thanks again.
    LVL 17

    Assisted Solution

    a target="_blank" should not be treated as a popup because the user clicked a link, its expected that something should and will happen.  On the other hand if you used to create a popup then it would most likely be blocked by a popup-blocker.

    target="_blank" is not javascript, it is pure html in the <a> tag, so even if the user has javascript turned off it will work.

    The thing is you cannot control the size of the window this way as far as I know.  If you do use javascript, you could control the size with a to make a popup.

    Snoyes has some excellent suggestions.

    Joe P
    LVL 1

    Expert Comment

    You could just have the link direct them to click.php which records the cilck them do a header redirect to direct them to were you wish them to go or (go back)



    //record the click

    header("Location:" .  $_SERVER['HTTP_REFERER']);
    LVL 17

    Expert Comment

    Snoyes first post mentions target="_blank".  I just clarified some questions the author had after hearing it from snoyes.  He probably deserves the points.  Or at least a split.

    Joe P

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now