Call PHP Function With Onclick Event

Call php function with onclick event?

Just follow this code to achieve using jQuery ajax call.
This is the HTML code with jQuery included and I've used test.php file in the same directory. Just write your PHP function in test.php file. Finally the response will be displayed in the given div after the user pressed confirm.

<!DOCTYPE html>
<html>
<head>
<title>Jquery ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button onclick="myFun()">Click me</button>
<div id="div1"></div>
<script>
function myFun(){
var r= confirm("Press a button!");
if (r==true)
{
$.ajax({url: "test.php", success: function(result){
$("#div1").html(result);
}});
}
else
{
alert("You pressed Cancel!");

}
}
</script>
</body>
</html>

call php function with onclick event

A jquery demo is as follows:-

abc.php:- (you can change extension to .html also if you are not going to use any php code in this file)

<button class="fa fa-angle-right" onclick="myFunction();">Click Me Please!</button>

<script src= "https://code.jquery.com/jquery-3.1.1.min.js"></script>

<script type="text/javascript">
function myFunction(){

$.ajax({url: "demo_test.php", success: function(result){
alert(result);
}});
}
</script>

demo_test.php:-

<?php
include_once('def.php');

$obj = new myClass();

$result = $obj->method1();

echo $result;
?>

def.php:-

<?php
class myClass{
function method1(){
echo "demo";
}
function method2(){
echo "demo2";
}
}

Output:- http://prntscr.com/clw7hu and http://prntscr.com/clw7sl

Note:-

All three files must be at same working location(same directory)

It's a very small and easy sample to understand what is exactly need and how it can performed. Thanks

Exectue PHP Function from <button onclick="">

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/

How to call a PHP function on the click of a button

Button clicks are client side whereas PHP is executed server side, but you can achieve this by using Ajax:

$('.button').click(function() {
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});

In your PHP file:

<?php
function abc($name){
// Your code here
}
?>

how to call a PHP function with HTML button click?

Like this?

<?php
if (isset($_POST['submit'])) {
someFunction();
}
function someFunction() {
echo 'HI';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<input type="submit" name="submit">
</form>
</body>
</html>

Although quite weird, this will work, you can use $_GET if you want

Call PHP function with button onClick with parameter/ AJAX?

You should call a js-function with onclick event like this:

<input type='button' name='Release' onclick="downloadFichier(param1, param2)" value='Click to Release'>

And your AJAX-function should be like this:

function downloadFichier(param1, param2){

$.ajax({
type: 'POST',
url: 'file_product.php',
data: "param1=" + param1 + "¶m2=" + param2,
success: function(data) {
$("p").text(data);
}
});

You can get your params in PHP-script from the $_REQUEST array by their names (param1, param2).

Execute PHP function with Javascript onClick without page refresh

You can get the input value with jQuery before submitting:

<form action="output.php" method="post" id="form">
<input name="city" id="city_input">
<input type="submit" value="Button" />
</form>
<script>
$('#form').submit(function(e) {
e.preventDefault();
let cityInputVal = $('#city_input').val();
if (confirm('Do you want to submit ' + cityInputVal + '?'))
{
this.submit();
} else {
return false;
}
});
</script>

The preventDefault() function stops the form submission and therefore, page refresh.



Related Topics



Leave a reply



Submit