1

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?

2
  • 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 Commented Apr 15, 2020 at 8:00
  • Can the following work: HTML: <button id="firstButton" type="submit">Button</button> JQUERY: $("#firstButton").submit(function(event){ } Do you think this could work or am i totally wrong? Commented Apr 15, 2020 at 8:33

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

3 Comments

well this is working fine, but i changed the Code inside the Button2 Function and now its loading on website load...
u have changed button 2 of php or html .what u have changed. send the new code here .
` ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); $from = "[email protected]"; $to = "[email protected]"; $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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.