[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

call a php function from a button

Posted on 2007-10-13
7
Medium Priority
?
3,026 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:lesouef
  • 4
  • 3
7 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20070649
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.
0
 
LVL 28

Author Comment

by:lesouef
ID: 20071049
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?
0
 
LVL 28

Author Comment

by:lesouef
ID: 20071054
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Accepted Solution

by:
nizsmo earned 1000 total points
ID: 20071885
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:

http://www.digifuzz.net/archives/2007/07/phpajax-instant-db-query-and-results/

Let me know how you go.
0
 
LVL 28

Author Comment

by:lesouef
ID: 20073263
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.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20073272
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! :)
0
 
LVL 28

Author Comment

by:lesouef
ID: 20073298
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question