Yes, you need Ajax here. Please refer to the code below for more details.

 

Change your markup like this

<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />

 

jQuery:

$(document).ready(function(){
    $('.button').click(function(){
        var clickBtnValue = $(this).val();
        var ajaxurl = 'ajax.php',
        data =  {'action': clickBtnValue};
        $.post(ajaxurl, data, function (response) {
            // Response div goes here.
            alert("action performed successfully");
        });
    });
});

In ajax.php

<?php
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'insert':
                insert();
                break;
            case 'select':
                select();
                break;
        }
    }

    function select() {
        echo "The select function is called.";
        exit;
    }

    function insert() {
        echo "The insert function is called.";
        exit;
    }
?>
Answer from Roopendra on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-call-php-function-on-the-click-of-a-button
How to call PHP Function on the Click of a Button? - GeeksforGeeks
July 12, 2025 - </h4> <?php if(array_key_exist... is Button2 that is selected"; } ?> <form method="post"> <input type="submit" name="button1" class="button" value="Button1" /> <input type="submit" name="button2" class="button" value="Button2" ...
🌐
SitePoint
sitepoint.com › php
Html button in PHP calling PHP function? - PHP - SitePoint Forums | Web Development & Design Community
February 4, 2005 - morning to all, ive never tried to do this so don’t even know if PHP does this or not: i have a php script and this is how it ends: print"<center><br><table width=\“100%\” border=\“0\”> <tr> <td width=\“100%\” heigh…
Top answer
1 of 5
21

Having 2 files like you suggested would be the easiest solution.

For instance:

2 files solution:

index.html

(.. your html ..)
<form action="script.php" method="get">
  <input type="submit" value="Run me now!">
</form>
(...)

script.php

<?php
  echo "Hello world!"; // Your code here
?>

Single file solution:

index.php

<?php
  if (!empty($_GET['act'])) {
    echo "Hello world!"; //Your code here
  } else {
?>
(.. your html ..)
<form action="index.php" method="get">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Run me now!">
</form>
<?php
  }
?>
2 of 5
2

I know this question is 5 years old, but for anybody wondering how to do this without re-rendering the main page. This solution uses the dart editor/scripting language.

You could have an <object> tag that contains a data attribute. Make the <object> 1px by 1px and then use something like dart to dynamically change the <object>'s data attribute which re-renders the data in the 1px by 1px object.

HTML Script:

<object id="external_source" type="text/html" data="" width="1px" height="1px">
</object>

<button id="button1" type="button">Start Script</button>

<script async type="application/dart" src="dartScript.dart"></script>
<script async src="packages/browser/dart.js"></script>

someScript.php:

<?php
echo 'hello world';
?>

dartScript.dart:

import 'dart:html';

InputElement button1;
ObjectElement externalSource;

void main() {
    button1 = querySelector('#button1')
        ..onClick.listen(runExternalSource);

    externalSource = querySelector('#external_source');
}

void runExternalSource(Event e) {
    externalSource.setAttribute('data', 'someScript.php');
}

So long as you aren't posting any information and you are just looking to run a script, this should work just fine.

Just build the dart script using "pub Build(generate JS)" and then upload the package onto your server.

🌐
Stack Overflow
stackoverflow.com › questions › 48207368 › how-to-put-php-code-into-html-button
How to put php code into html button
</head> <body> <div class="container-contact100"> <div class="wrap-contact150"> <form class="contact150-form validate-form"> <div class="row"> <div> <h1><br>HOME AUTOMATION</br></h1> </div> </div> <div class="row"> <div> <h3><br>LAMPU</br></h3> </div> </div> <div class="row"> <div> <td><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p> </td> </div> </div> <div class="row"> <div> <br><button class="contact100-form-btn"> On </button></br> </div> <div class="col-sm-5"> <br><button class="contact100-form-btn"> Off </button></br> </div> </div> </form> <?php include 'connect.php'; func
Find elsewhere
🌐
SitePoint
sitepoint.com › php
HTML Button, how to run php file with a parameter - PHP - SitePoint Forums | Web Development & Design Community
September 28, 2018 - Hi everyone, My php code is consisted of 2 buttons as follows: <!DOCTYPE HTML> <html> <body> <button type="submit" onclick="myPHP.php/'1'">Click Me!</button> <button type="submit" onclick="myPHP.php/'2'">Click Me!</but…
🌐
CodeProject
codeproject.com › Questions › 780440 › calling-a-function-of-php-using-html-button
[Solved] calling a function of php using html button
May 30, 2014 - Interactive coding education emphasizes hands-on practice over passive lecture consumption through in-browser coding environments providing immediate feedback. Popular platforms like Codecademy, freeCodeCamp, The Odin Project, and Khan Academy offer structured learn programming curricula guiding beginners from fundamentals to project completion.
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
PHP coded button - PHP Coding Help - PHP Freaks
February 16, 2023 - How can I create a PHP coded button which is responsive without using the form tag on HTML
🌐
W3Schools
w3schools.com › php › php_form_complete.asp
PHP Complete Form Example
To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags.
🌐
Quora
quora.com › How-can-I-call-a-PHP-file-using-an-HTML-button
How to call a PHP file using an HTML button - Quora
If the php file is in the same folder then you can just pass the name of the file in the anchor tag before html button like <a href=”file.php”><button></button></a>. But this request will change the url in the url bar and it will block the ...
🌐
Stack Overflow
stackoverflow.com › questions › 36567591 › how-to-create-a-html-button-that-runs-a-php-script
How to create a html button that runs a php script? - Stack Overflow
<input type="button" value="Run Script" onclick="run_script();"/> <script> function run_() { $.post("yourScripFile.php",function(data){ if(data != null) { alert(data); } }); } </script> ... <?php $user = JFactory::getUser(); $userToken = ...
🌐
Sololearn
sololearn.com › en › Discuss › 1708320 › open-an-html-file-with-php-buttons
Open an HTML file with PHP buttons | Sololearn: Learn to code for FREE!
Let's say I have 3 files: home.html, q-a.html and contact.html How do I (I am on the home.html) go to the contact.html by pressing a button using php · phpopenpage · 1st Mar 2019, 10:04 PM · Roel · 7 Answers · Answer · + 6 · <?php echo "<button><a href='contect.html'>Contact</a></button>"; ?> 1st Mar 2019, 10:16 PM ·
🌐
Delft Stack
delftstack.com › home › howto › php › onclick php
How to Execute PHP Function With onclick | Delft Stack
February 2, 2024 - For example, write a PHP function php_func() that displays the message Stay Safe. Create a button with the name Click using the button tag. Specify the onclick() function as an attribute with the clickMe() function as its value.
🌐
Programming Head
programminghead.com › how-to-link-submit-button-to-another-page-in-php
How to Link Submit Button to Another Page in PHP [Updated]
In HTML's Form tag we can Link ... user clicks the Form Button, It will redirect that user to another page. ... We can use Anchor <a> tags to Link a Submit button to another page in PHP....
🌐
PHPHelp
phphelp.com › beginners - learning php
HTML Button, how to run php file with a parameter - Beginners - Learning PHP - PHPHelp
September 28, 2018 - Hi everyone, My php code is consisted of 2 buttons as follows: <!DOCTYPE HTML> <html> <body> <button type="submit" onclick="myPHP.php/'1'">Click Me!</button> <button type="submit" onclick="myPHP.php/'2'">Click Me!</but…
🌐
W3Schools
w3schools.com › tags › tag_button.asp
HTML button tag
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.