Jump to content

hyperlink to perform action before link is visited


jasonc

Recommended Posts

been told that ui need to use AJAX to perform the following.

link being click would increase a counter in my DB before the link it visited.

 

<a href="www.site1.com" (javascript code - need to be able to pass the url to the DB so it updates the counter for that url)>site 1</a>

 

can anyone suggest a way of doing this ?

 

thanks in advance

Link to comment
Share on other sites

I'll tell you, but it's not easy.

 

You have to use javascript to create an HTTP request to a file on the server. The file on the server should update the database as necessary. You can just leave it at that, and your page will forward after shooting off the request to the server.

 

If you don't know how to do any of the above, you can google 'ajax tutorials' and they will tell you how. You will need to understand at least basic javascript, a server-side language, and how to interact with a database using said server-side language.

Link to comment
Share on other sites

Why not just use internal-external links.

 

For example:

 

<a href="links.php?id=1">SomeSite.com</a>

 

Then, links.php would know that id 1 is somesite.com somehow (DB or something).  It would increment the counter and send the client on his way.

 

 

(Or, you could even do ?url=somesite.com)

 

This could of course be done with AJAX, but why make it more difficult than need be?

Link to comment
Share on other sites

i am already using this method, but it needs to be done the other difficult way.

 

needs must?!

 

shame really as i thought i had the perfect way!

 

but it now needs to be done the other way.

 

thanks haku..

 

i searched ajax tutorials but not sure what one to start with, tried a few but again no idea exactly what this process would be under.

 

please can you suggest a link that will take me to what i ned to be loking at to do what i am after

 

thanks

Link to comment
Share on other sites

You can do it like this:

 

1) user clicks the link

2) Javascript creates an XMLHttpRequestObject

3) Request object accesses update.php on the server

4) Update PHP has a DB command that says "update COUNTER where page='page_name'"

5) Return TRUE to the link

6) The page will follow the address in the link

 

I can't point you at any specific tutorials. I write my own ajax commands when I need them, so I'm not too up-to-date on what tutorials are out there.

 

Truth be told though, using AJAX is unreliable, as users may have javascript turned off. Corbin's solution of forwarding the user to a page that updates the database, and then forward them to the page they want is a good one. They will never see the update page, so it won't matter to them. And they can't turn this functionality off ,though they can go direct to the page without using your links, and the DB won't be updated.

 

If you want something to be completely reliable, then you should write a server-side function that updates the database, and call it at the top of EVERY page. That way you can track each page individually, and it will get called no matter what, which gives it absolute reliability.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.