I have a page.php and a func.php. I know that the request that I can do is with AJAX, but I don't know how do it. I searched here and I found this, but I don't understand how it works exactly.
This is my files:
page.php
<html>
<head>
<script src="func.php" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
function hello(){
$.ajax({
url: 'func.php',
data: {action: 'hello'},
type: 'post',
success: function(output) {
alert(output);
}
});
}
function bye(){
$.ajax({
url: 'func.php',
data: {action: 'bye'},
type: 'post',
success: function(output) {
alert(output);
}
});
}
</script>
</head>
<body>
<form action method="post" name="form" id="form" action="javascript:hello();" >
<button type="input">Hello</button>
</form>
<form action method="post" name="form" id="form" action="javascript:bye();" >
<button type="input">Bye</button>
</form>
</body>
</html>
func.php
<?
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'hello' : hello();break;
case 'bye' : bye();break;
}
function hello(){
echo "Hello";
}
function bye(){
echo "bye";
}
}
?>
The code is very simple, I only want that when a user click the button "hello" or "bye", the javascript call the function and print the result. This is a example of my real code, I know that if I want print "hello/bye" I don't need ajax or other file.
Thanks a lot!