How To Execute Php Function On Html Button Click May 30, 2024 Post a Comment Hello I want to execute bb() function on button click. I tried following code but it did not work. echo ''; echo 'Solution 1: Your button is HTML and your function is PHP. They look like together because they are in the same file, but they are not together. PHP exists only on the server. HTML only works on the client (browser). When you see the button on your browser, the PHP is gone, you only have HTML.To make a HTML button to call a PHP function, you will have to move your function to a PHP file, then make your button to call it with Ajax. Example:bb1.html : contains button that uses Ajax to call PHP function.<html><head><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><scripttype = "text/javascript">functionmyAjax () { $.ajax( { type : 'POST', data : { }, url : 'bb2.php', // <=== CALL THE PHP FUNCTION HERE.success: function ( data ) { alert( data ); // <=== VALUE RETURNED FROM FUNCTION. }, error: function ( xhr ) { alert( "error" ); } }); } </script></head><body><buttononclick="myAjax()">Click here</button><!-- BUTTON CALL PHP FUNCTION --></body></html>Copybb2.php : contains function that returns "hello".<?phpfunctionbb() { echo"hello"; // VALUE RETURNED. } bb(); ?>CopyCreate two text files with the given names, copy-paste this codes, open your browser and run "localhost/bb1.html".This is how a button calls a PHP function : Ajax does all the magic. Share Post a Comment for "How To Execute Php Function On Html Button Click"
Post a Comment for "How To Execute Php Function On Html Button Click"