Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-05-2010, 10:17 PM Onclick with php
Average Talker

Posts: 27
Trades: 0
Hello, I wanted to set up a downloads counter on my web, and in order to do that I want to use onClick action that will access SQL and add one to the counter. Now here is the problem.
how do I execute php code when users clicks on download button?
Can anyone show me code or at least provide some hints on how to do that
__________________
Those who can: learn. Those who can't: teach.
Cinatas is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-06-2010, 01:57 AM Re: Onclick with php
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
Look into Javascript database functions. Javascript isn't my area so the only additional information I can provide would be from google.
Stephen. is offline
Reply With Quote
View Public Profile
 
Old 01-06-2010, 05:58 AM Re: Onclick with php
Ultra Talker

Posts: 339
Trades: 0
Where does download button leads to? If it leads to file like download.php?file_id=xxx, you can easily add a few lines of PHP code to download.php file to count downloads.
__________________
Daniel, Helpdesk Leader

Please login or register to view this content. Registration is FREE
- First class web hosting services.

Please login or register to view this content. Registration is FREE
- Provide unlimited disk space and bandwidth!
Hosting24 is offline
Reply With Quote
View Public Profile
 
Old 01-06-2010, 07:23 AM Re: Onclick with php
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I believe Hosting24's proposal would be best, since it doesn't require javascript. Have your link go to download.php with a given id. That page would look up which file it is in a database and serve it via php, by first sending (printing) the correct file headers and then use readfile().

For example, if the current file is an image, you would send a header with mime-type image/jpeg, then call readfile() with the image path. The user's address bar would say something like domain.com/download.php?id=xxx but the browser would display an image, just as if the address were domain.com/image.jpg

This way would also "protect" your files' actual location on the server, since the user only sees a file id, not a location. It makes an additional defense when users can upload files, because people can't upload disguised harmful php files and run them by accessing their location.

-------------------------------------------------------------------------------------------

Otherwise you could use AJAX to call a php page with an onclick, which would then incrase the counter. I highly recommend the javascript library jQuery for this. I'm not good with javascript but jQuery was easy evan for me
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-06-2010, 01:19 PM Re: Onclick with php
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Just remember, PHP is a pre-processing language. The acronym is a bit nonsensical, but it helps convey the meaning:

PHP = PHP Hypertext Pre-Processor

This means all PHP is done executing by the time anything hits the browser. If you want to then add events or behaviors, you can't, at least not with PHP, since its interpreter has exited. PHP is a server-side language only. If you want something to happen after there is a website viewable in the browser, you need a client-side language.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-06-2010, 06:38 PM Re: Onclick with php
Stephen.'s Avatar
Average Talker

Posts: 22
Name: Stephen
Trades: 0
Quote:
Originally Posted by lizciz View Post
I believe Hosting24's proposal would be best, since it doesn't require javascript. Have your link go to download.php with a given id. That page would look up which file it is in a database and serve it via php, by first sending (printing) the correct file headers and then use readfile().

For example, if the current file is an image, you would send a header with mime-type image/jpeg, then call readfile() with the image path. The user's address bar would say something like domain.com/download.php?id=xxx but the browser would display an image, just as if the address were domain.com/image.jpg

This way would also "protect" your files' actual location on the server, since the user only sees a file id, not a location. It makes an additional defense when users can upload files, because people can't upload disguised harmful php files and run them by accessing their location.

-------------------------------------------------------------------------------------------

Otherwise you could use AJAX to call a php page with an onclick, which would then incrase the counter. I highly recommend the javascript library jQuery for this. I'm not good with javascript but jQuery was easy evan for me
Agreed with this, AJAX can be quite complex to a first time user which I believe you will be if you haven't concidered it yet. jQuery is a very useful resource and there is lots of help if you search through google. If you do wish to keep filesize down though then I'd still recommend googleing the onclick function for javascript as it is only a few lines of code instead of the larger file of jQuery
Stephen. is offline
Reply With Quote
View Public Profile
 
Old 01-06-2010, 11:11 PM Re: Onclick with php
Average Talker

Posts: 27
Trades: 0
Thanks a lot to all who helped.
I went with hosting's method and it worked like magic, so thanks a lot hosting's
__________________
Those who can: learn. Those who can't: teach.
Cinatas is offline
Reply With Quote
View Public Profile
 
Old 01-07-2010, 03:14 AM Re: Onclick with php
Ultra Talker

Posts: 339
Trades: 0
No problems. Sometimes the best solution is the most simple one
__________________
Daniel, Helpdesk Leader

Please login or register to view this content. Registration is FREE
- First class web hosting services.

Please login or register to view this content. Registration is FREE
- Provide unlimited disk space and bandwidth!
Hosting24 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Onclick with php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24605 seconds with 12 queries