Ajax File
Create new php file named ajax.php as below:
<?php
$student = array(
'id' => 'st01',
'name' => 'name 1',
'age' => 20
);
header('Content-Type: application/json');
echo json_encode($student);
?>
Index File
Create new php file named index.php as below:
<html>
<head>
<title>nilpointer.net</title>
<script src="js/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#buttonDemo').click(function() {
$.ajax({
type: 'GET',
url: 'ajax.php',
success: function(student) {
var result = 'id: ' + student.id;
result += '<br>name: ' + student.name;
result += '<br>age: ' + student.age;
$('#result').html(result);
}
});
});
});
</script>
</head>
<body>
<input type="button" value="Demo Ajax" id="buttonDemo">
<br>
<span id="result"></span>
</body>
</html>