Call PHP function on Button click using jquery ajax


Posted on : Wed, 22nd Apr 15 01:57 pm (Updated on : Wed, 22nd Apr 15 01:57 pm) UTC
Author : Code2care
100+ votes

This can be achieved using ajax, lets see an example,

index.html Language : Html
<html>
<head>
<title>Example</title>


<script>


$('.myButton').click(function() {

 $.ajax({
  type: "POST",
  url: "phpFileWithFunction.php"
}).done(function( resp ) {
  alert( "Hello! " + resp );
});    

});

</script>
</head>
<body>

<input type="button" value="Click" id="myButton" />
</body>


Advertisements





File : phpFileWithFunction.php Language : Php
<?php

class Foo {
	
function myFunc() {
	
	return "This data is received from PHP function on button click!";
}



}

$fooObj = new Foo();

echo $fooObj->myFunc();

?>

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +