-2

I'd like to try to call a function written in C/C++ after clicking a button on a website. For example, say I have a website with an image and a button; after clicking the button, I'd like to run a C routine that maybe performs some filtering (perhaps a lowpass/blur filter) and then updates the image with the blurred image (preferably without reloading the whole website).

My understanding is you can't do this directly in javascript, since it's run on client side. I know you can call C functions with php, but my understanding is that php is parsed/run when the website is loaded, and then php is essentially done (although this may be wrong or incomplete).

So, what general path should I take to call this C function as a button callback, have it run on the server, and then return and update an image. I'd like to stick with just major languages if possible, like php, javascript, maybe jquery or AJAX if need be, etc.

JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
  • You need to show that you have done some research and some code for us to help you answer your problem. – cmorrissey Sep 03 '13 at 14:06
  • 2
    You could try AJAX. That way you can call your PHP function that triggers the C function. – putvande Sep 03 '13 at 14:07
  • @ChristopherMorrissey This is for a project I'd like to do on the side for fun (literally I just enjoy doing this stuff as a hobby). This isn't an HW assignment or anything. Since I'm new to php/javascript I'd like to get a few tips to point me in the right direction so I don't get completely lost, but in retrospect maybe I should have researched it a little more before posting. – JustinBlaber Sep 03 '13 at 14:16

3 Answers3

4

If you know how to call you c function from php, then you are almost done.

"without reloading the whole website" means usage of AJAX. Easiest way would probably be to use JQuery.ajax to perform the ajax request, which fetches the result from the server and then places it into the page.

Michal
  • 1,262
  • 1
  • 12
  • 22
3
      Server               |        Client      
---------------------------------------
                           |   Client requests page
             /-------------+-------------/ 
PHP generates a GUI and    |
sends HTML/javascript to   |
client to tell browser what|
to display                 |
         \-----------------+---------\    
                           |        User uses GUI to issue a command
                           |        one that should run a server side action
                           |        an ajax request is sent to inform the server 
                           |        of the request 
    /----------------------+-----------/
Server recuieves an ajax   |
request, this tells the    |
server to run a command    |
The command is run and the |
server returns to the user |
updated data               |
   \-----------------------+--------------\
                           |    Updated data is recieve by the browser and capture as
                           |    a return on a ajax request. The image is updated as per
                           |    the users expectations.                       
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
  • This is great. So if I were to follow something like this (the top answer): http://stackoverflow.com/questions/7165395/call-php-function-from-javascript, but also figure out how to call a C function from php, then I would be close to what I'd like to accomplish? – JustinBlaber Sep 03 '13 at 14:24
0

If I needed to do this, I would use node.js (javascript), and write a node.js addon that wrapped my C function, then call it through a rest API of some sort:

http://www.nodejs.org/api/addons.html

Ricky Nelson
  • 876
  • 10
  • 24