HTML代码
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="../jQuery v2.2.0.js"></script>
<script type="text/javascript">
$(function(){
//按钮单击时执行
$("#testAjax").click(function(){
//Ajax调用处理
$.ajax({
type: "POST",
url: "1.php",
data: "name=Tom&age=18",
success: function(data){
$("#myDiv").html('<h2>'+data+'</h2>');
}
});
});
});
</script>
</head>
<body>
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="testAjax" type="button">Ajax改变内容</button>
</body>
</html>
PHP代码
<?php
/*
$name=$_POST["name"];
$age=$_POST["age"];
echo "姓名".$name;
echo "年龄".$age;
*/
?>
<?php
$name = isset( $_POST[ 'name' ] ) ? htmlspecialchars( $_POST[ 'name' ] ) : '';
$age = isset( $_POST[ 'age' ] ) ? htmlspecialchars( $_POST[ 'age' ] ) : '';
echo '姓名: ' . $name;
echo "
";
echo '年龄: ' . $age;
?>