There already are some entries about the topic explaining that the only way is by using Ajax. However, I do not find solution for this problem: I just want that, once pressing a button in an HTML file, run a PHP function.
Imagine I have two buttons button1
button2
so I want each of them to run function run1()
function run2()
of my PHP file.
How the Ajax would look like? This is what I have.
HTML:
<script> // For button1
$(document).ready(function(){
$("#button1").click(function(){
var value = $("#button1").val();
$.ajax({
url: 'myPHP.php',
type: 'POST',
data: {button:value},
success: function(response){
$('#output').append(response);
}//end success
}); //end ajax
});
});
</script>
<body>
<form method="post" action="myPHP.php">
<button id= "button1" type="submit" value="value1">Button One</button>
<button id= "button2" type="submit" value="value2">Button Two</button>
</form>
</body>
PHP:
if($_POST['button'] == 'value1'){
// run function1;
}else{
// run function2;
}