這是一種方式通過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"));
}
如果你有經驗與jQuery這應該是一件容易的事。如果沒有,學習JQuery很簡單:) http://api.jquery.com/jQuery.post/ – CoBolt 2013-03-18 07:38:50