jasonc Posted August 20, 2008 Share Posted August 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
haku Posted August 21, 2008 Share Posted August 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 21, 2008 Share Posted August 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
jasonc Posted August 21, 2008 Author Share Posted August 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
haku Posted August 22, 2008 Share Posted August 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 22, 2008 Share Posted August 22, 2008 "i am already using this method, but it needs to be done the other difficult way." Why does it need to be done this way? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.