0

I want to execute a PHP Script, when i Click an HTML Button. I know, that i cant do this: <button onclick="myPhpFunction("testString")">Button</button> or something similar. My goal is, that i execute some Commands on my Server when clicking this Button. Note: My php Function need variables from the JS/HTML.

Is there a way to do this?

yama_HD
  • 480
  • 1
  • 4
  • 14
  • 2
    You should send an AJAX request to the Server and run PHP function from there. Javascript is running on the user's machine while PHP is located on the server. For example, like this [jQuery Ajax Post](https://stackoverflow.com/a/5004276/2392957) – alexey-novikov Apr 15 '20 at 08:00
  • Can the following work: HTML: `` JQUERY: `$("#firstButton").submit(function(event){ }` Do you think this could work or am i totally wrong? – yama_HD Apr 15 '20 at 08:33

1 Answers1

0

already answered here :- Run php function on button click

one more example from my side:-

How to call PHP function on the click of a Button ?

<h1 style="color:green;"> 
    GeeksforGeeks 
</h1> 

<h4> 
    How to call PHP function 
    on the click of a Button ? 
</h4> 

<?php
    if(array_key_exists('button1', $_POST)) { 
        button1(); 
    } 
    else if(array_key_exists('button2', $_POST)) { 
        button2(); 
    } 
    function button1() { 
        echo "This is Button1 that is selected"; 
    } 
    function button2() { 
        echo "This 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" /> 
</form> 

source:-https://www.geeksforgeeks.org/how-to-call-php-function-on-the-click-of-a-button/

user12449933
  • 170
  • 1
  • 6
  • well this is working fine, but i changed the Code inside the Button2 Function and now its loading on website load... – yama_HD Apr 15 '20 at 10:30
  • u have changed button 2 of php or html .what u have changed. send the new code here . – user12449933 Apr 15 '20 at 10:32
  • ` ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); $from = "test@example.de"; $to = "test@example.de"; $subject = "Title"; $message = "Body"; $headers = "From:" . $from; mail($to,$subject,$message, $headers); echo "Test email sent";` So this Code is perfectly working, but i dont want it to load on start of the Page. – yama_HD Apr 15 '20 at 10:53