I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name
.
I tried, but it's not showing,
Server-side
The PHP file name is "name.php" having function greet()
with parameter $name
is as follows:
<?php
function greet($name)
{
echo "hello $name";
}
?>
Client-side
The HTML file consists of a button "Click me" which should send the name John
to PHP page, and the greet()
function should execute and output should display at client side as "Hello John" is as follows:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("#button").click(function(){
$.ajax({
type: "POST",
url: "name.php",
data: { name: "John" }
}).done(greet(data)
{
alert( "Data Saved: " + data);
});
});
});
</script>
<input type="button" id="button" value="Click me">
</html>
I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.
Can someone please help to how to get output from PHP function to client-side on button click.