一、說明
在AjAX送出到PHP顯示相關內容
二、PHP語法
require_once("DB1.php");
$PG=0;
if(isset($_POST["k1"])){
$PG=$_POST["k1"];
}elseif(isset($_GET["k1"])){
$PG=$_GET["k1"];
}
mysql_select_db($database_DB1, $DB1);
$query_Re1_menu = "SELECT * FROM text1 WHERE t1_id=".$PG;
$Re1_menu = mysql_query($query_Re1_menu, $DB1) or die(mysql_error());
$totalRows_Re1_menu = mysql_num_rows($Re1_menu);
while($r = mysql_fetch_assoc($Re1_menu)){
$output[]=$r;
}
print(json_encode($output,JSON_UNESCAPED_UNICODE));
mysql_free_result($Re1_menu);
三、jQuery語法
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AJAX 語法教學</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script language="javascript">
$(document).ready(function(){
$("#button").click(function(){
$.ajax({
type:"POST",
url: "ajax_1_post.php",
data: {k1:1},
error: function(data){
alert("POST出錯");
},
success: function(data){
$("#text1").html(data);//全部陣列內容
}
});
});
$("#button2").click(function(){
$.ajax({
type:"GET",
url: "ajax_1_post.php",
data: {k1:2},
error: function(data){
alert("GET出錯");
},
success: function(data){
$("#text1").html(data);//全部陣列內容
}
});
});
});
</script>
</head>
<body>
<input type="button" name="button" id="button" value="POST送出">
<input type="button" name="button2" id="button2" value="GET送出">
<div id="text1">顯示</div>
</body>
</html>
留言列表