4 Replies - 16384 Views - Last Post: 23 March 2011 - 05:22 AM Rate Topic: -----

#1 tusharharlikar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 11

how to call php class function on button click in html

Posted 22 March 2011 - 11:08 PM

i create in separate html page and php class ,in php class i create one function named as save so i want to call this function on button event i html
Is This A Good Question/Topic? 0
  • +

Replies To: how to call php class function on button click in html

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,347
  • Joined: 08-June 10

Re: how to call php class function on button click in html

Posted 23 March 2011 - 12:05 AM

you canít. PHP is executed on the server and HTML/Javascript on the client/browser. the only thing you can do is write a PHP script that does the saving and call that (submit action or AJAX call).
Was This Post Helpful? 0
  • +
  • -

#3 chinchang   User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 727
  • Joined: 22-December 08

Re: how to call php class function on button click in html

Posted 23 March 2011 - 12:06 AM

You can attach a listener function to the button's click event using javascript and inside that listener, use AJAX to call that php file which contains the function.

But remember, if you want to make any changes in the html on button click, you will have to do it all through javascript.


Using Jquery, you can easily do all the above mentioned stuff.
Was This Post Helpful? 0
  • +
  • -

#4 tusharharlikar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 11

Re: how to call php class function on button click in html

Posted 23 March 2011 - 02:28 AM

can any one give example on this
Was This Post Helpful? 0
  • +
  • -

#5 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: how to call php class function on button click in html

Posted 23 March 2011 - 05:22 AM

Using AJAX to send data to scripts is quite easy.

Reading up on this would help you out http://www.w3schools...jax/default.asp

Sample Code.
<script type="text/javascript">
var xmlhttp = new XMLHTTPREQUEST();
xmlhttp.onreadystatechange = function() {
	if(readyState == 4){
		var response = xmlhttp.responseText;
		alert(response); //or do whatever with it
	}
}

xmlhttp.open("GET", "phpscript.php?foo=bar&bar=foo", true); //encode variables to URL
xmlhttp.send();
</script>



phpscript.php
$foo = $_GET['foo'];
$bar = $_GET['bar'];
echo "$foo is not $bar"; //you can do whatever with them but whatever you echo or print will be returned to your AJAX script


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1