Current location: Hot Scripts Forums » Programming Languages » PHP » how to call a php function while click on html button?


how to call a php function while click on html button?

Reply
  #1 (permalink)  
Old 05-05-10, 07:09 PM
dewan dewan is offline
Newbie Coder
 
Join Date: Apr 2010
Posts: 20
Thanks: 4
Thanked 0 Times in 0 Posts
how to call a php function while click on html button?

hello there,
is there any way to call a php function while click on html button?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-06-10, 04:17 AM
dnraj.ch dnraj.ch is offline
New Member
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Php is purely a server scripting language . so, one can send request to the server to callback the function using some client side technologies eg. JAVASCRIPT as mouse ,keyboard events can be handled by brower related technologies... You can request the php script to execute a function using javascript while clicking on html button..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-06-10, 11:32 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Yes you can call a PHP function when you click on an HTML button.
You will have to use the onclick event listener in the button to call a Javascript function that will call the PHP function.
One thing to note though, if any variables are declared as GLOBAL in the PHP function, they must be assigned values before the Javascript function is parsed by the HTML interpreter.

Example of incorrect way to do it:
PHP Code:

<script>
function echoHello()
{
 alert("<?php hello(); ?>");
 }
</script>
<?php
$a
="hello";
function 
hello()
{
 global 
$a;
 echo 
$a;
 }
?>
<button onclick="echoHello()">Say Hello</button>
The above example will display an empty alert box.

Example of correct way to do it.
PHP Code:

<?php
$a
="hello";
?>
<script>
function echoHello()
{
 alert("<?php hello(); ?>");
 }
</script>
<?php
function hello()
{
 global 
$a;
 echo 
$a;
 }
?>
<button onclick="echoHello()">Say Hello</button>
You could also use Javascript's AJAX to do an httpRequest to a PHP file to perform the PHP tasks and return the results.

But you can not call a PHP function directly from a button using the onclick event listener.
Because the event listeners in an HTML element, only communicate with Javascript.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
html tutoral thefrtman HTML/XHTML/XML 5 04-27-09 11:25 AM
[SOLVED] delay on html element properties? UnrealEd JavaScript 5 05-05-08 04:23 AM
PHP Fatal error: Call to a member function item() on a non-object mohit PHP 3 08-25-06 10:35 AM
how do i call the php function inside the html tag selvam.cit PHP 6 08-24-06 09:25 PM
Please help insert html into php idforforums PHP 5 04-07-05 12:29 AM


All times are GMT -5. The time now is 03:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.