call a php function from a button

how can I call a php function from a button (onClick=....)?
it works if I call an external php script, using javascript:location.href=... but not when calling a function declared in the same file.
php function is ok and works if called within the php code.
the only thing I could do was to trigger a javascript function which does an echo of all php lines. pretty heavy...
does anyone has a smarter way?
LVL 28
Who is Participating?
nizsmoConnect With a Mentor DeveloperCommented:
No in most cases, 99% of the time javascript cannot talk to mysql directly, however yes you can use javascript and xmlhttprequest to call upon a php function which executes (and returns true?) and maybe then displays "operation successful" or something alike upon successful execution.

If you are interested, I have found this article particularly easy and useful to understand and adapt the code to your own use:

Let me know how you go.
Yes you can do this, however not any normal way, since onclick and javascript are both client-side scripting, while php is server-side scripting. To do this you will need to invoke a javascript function which then invokes a php function. then the php function returns something and javascript will update whatever it returns, and this is commonly known as AJAX.

Let me know if you need to know any more.
lesouefAuthor Commented:
do you mean I should use Ajax or this is the principle of ajax where the server uses javascript to update the client screen? I am not (as you probaly realized) a web man, I only know databses but I do occasional things on the web (difficult to bypass nowadays) mainyly php.
the return I expect from php is only true or false, I could even forget about it, it always works, and the app is not using important data. so how do I call a php function from a javascript then? (if I know php a bit, I almost ignore javascript, I use it with the book on my knees (not true, the js doc on my left screen).
is there a call/exec command? how to distinguish external php scripts from internal php functions? and is thre a way to stay in the same web page during execution to minimize blank screens?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

lesouefAuthor Commented:
forget to mention that I use php to write to mysql mainly, can js talk to mysql directly if the mysql port (def: 3306) is open to public?
lesouefAuthor Commented:
this is it, provided I can understand what this XMLHttpRequest does... do I need something special on my server to respond to this?
I'll copy the code locally to re-read it (such awful colors! maybe I should give this guy a few readability courses!).
and a philosophic remark, it is really too much energy to spend to get a name displayed, someting which is done with 2 lines of sql, 2 lines of servoy, 2 lines of filemaker.
The web definitly needs a query language which is something else than a bunch of tricks and workaround, no serious or complex app can be done this way unless you spend 2 years on it, and then come up with a huge price nobody would buy.
anyway, thanks for confirming there is no easy way to do this, I'll use yr example for my own understanding, but no way I do apps using this. the web world is rather weird, wants to be transparent and user friendly like classic apps, but has no language adapted for this.
Personally I think PHP is rather neat :-) And Ajax after you get used to it is not awefully complicated, especially if you have readymade code to modify.

All in all, good luck! :)
lesouefAuthor Commented:
I agree, php on its own is clean, but does not control anything on the UI unless you re-display the whole page at every query. still a bit old fashioned to do an application in 5 days...
None of these things are complex on their own, it's just that it makes dev very lengthy.
and thanks for the luck, I may need it!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.