これを行う方法は次のとおりですAJAX
ステップ 1
ここから jQuery ライブラリを取得します: https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js
ステップ 2 HTML ページ:
<html>
<head></head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
function myvalue() {
var a = 3;
$.ajax({
url: 'ajax_example.php'
cache: false,
type: 'GET',
data: {var: a},
dataType: 'JSON',
beforeSend: function() { alert("About to deploy ajax request");
success: function(response)
{
console.log(response);
if(response.success) {
alert(response.var);
} else {
alert(response.message);
}
}
});
$(document.ready(function() {
myvalue();
});
<script>
</body>
</html>
ステップ 3
PHP ページ ajax_example.php
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Content-Type: application/json; charset=utf-8');
$var = isset($_GET['var']) ? $_GET['var'] : FALSE;
if($var)
{
$var = htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
echo json_encode(array("success" => true, "var" => $var, "message" => "Example message"));
} else {
echo json_encode(array("success" => false, "message" => "You need to provide a value"));
}