Running php function on button click

I want to run the php function when a button is clicked. eg:

<input type="button" name="test" id="test" value="RUN"  onclick="<?php echo testfun(); ?>" /><br/>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}

?>

My question is that when I do this, I do not get the expected result that I was looking for. Please give me the best solution for this to run the php function on a button, whether it is simple buttonor click submit.

+7
source share
6 answers

I tried William's code, thanks brother.

but it doesn’t work like a simple button, I have to add a form with the method = "post". Also I have to write submit instead of a button.

here is my code below ..

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}

if(array_key_exists('test',$_POST)){
   testfun();
}

?>
+11
source

Do it:

<input type="button" name="test" id="test" value="RUN" /><br/>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}
if(array_key_exists('test',$_POST)){
   testfun();
}
?>
+2

javascript. PHP, , , :

    <form action="action_page.php">
       First name:<br>
       <input type="text" name="firstname" value="Mickey">
       <br>
       Last name:<br>
       <input type="text" name="lastname" value="Mouse">
       <br><br>
       <input type="submit" value="Submit">
     </form> 

( : http://www.w3schools.com/html/html_forms.asp)

, , "Ajax" - , Javascript-Way. , :)

+1
<a href="home.php?click=1" class="btn">Click me</a>
<?php 
  if($_GET['click']){
    doSomething();
  }
?>

JS ajax !

+1

onClick() - ,

<?php
echo '<br><Button onclick="document.getElementById(';?>'modal-wrapper2'<?php echo ').style.display=';?>'block'<?php echo '" name="comment" style="width:100px; color: white;background-color: black;border-radius: 10px; padding: 4px;">Show</button>';
?>
-1

isset().

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" />

</form>

<?php

function testfun()
{
   echo "Your test function on button click is working";
}

if(isset($_POST('submit')))
{
   testfun();
}

?>
-1

Source: https://habr.com/ru/post/1609312/


All Articles