Razor Razor - 1 year ago 102
PHP Question

How to Call a PHP Function on the Click of a Button

I am a rookie in PHP and have just started learning the basics of this language.

I have created a page called functioncalling.php that contains two buttons, Submit and Insert. As a beginner in PHP, I want to test which function is executed when a button gets clicked. I want the output to come on the same page. So I created two functions, one for each button. The source code for functioncalling.php is as follows:

<form action="functioncalling.php">
<input type="text" name="txt" />
<input type="submit" name="insert" value="insert" onclick="insert()" />
<input type="submit" name="select" value="select" onclick="select()" />
function select(){
echo "The select function is called.";
function insert(){
echo "The insert function is called.";

The problem here is that I don't get any output after any of the buttons are clicked.

Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.

Answer Source

Button click is client side whereas PHP is server side, but you can achieve this by using ajax

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

  type: "POST",
  url: "some.php",
  data: { name: "John" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );


In your php file:

function abc($name){
//your code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download