Execute PHP Function With Onclick

Execute PHP function with onclick

First, understand that you have three languages working together:

  • PHP: It only runs by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).

  • HTML & JavaScript: It only runs in someone's browser (excluding NodeJS).

I'm assuming your file looks something like:

<!DOCTYPE HTML>
<html>
<?php
function runMyFunction() {
echo 'I just ran a php function';
}

if (isset($_GET['hello'])) {
runMyFunction();
}
?>

Hello there!
<a href='index.php?hello=true'>Run PHP Function</a>
</html>

Because PHP only responds to requests (GET, POST, PUT, PATCH, and DELETE via $_REQUEST), this is how you have to run a PHP function even though they're in the same file. This gives you a level of security, "Should I run this script for this user or not?".

If you don't want to refresh the page, you can make a request to PHP without refreshing via a method called Asynchronous JavaScript and XML (AJAX).

That is something you can look up on YouTube though. Just search "jquery ajax"

I recommend Laravel to anyone new to start off right: http://laravel.com/

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

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>

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/

Run php function on button click

I tried the code of William, Thanks brother.

but it's not working as a simple button I have to add form with method="post". Also I have to write submit instead of 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();
}

?>

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

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
}
?>

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