PHP對應button中的onclick事件

王林
發布: 2023-04-07 09:02:02
轉載
4071 人瀏覽過

PHP是伺服器端程式碼 ,html是客戶端程式碼,實作button的onclick事件,就是客戶端呼叫伺服器端函數,因此就得向伺服器提交請求。

有一個簡單粗暴的方式,就是button是a標籤時,可以直接在href裡面寫上要執行的php頁面(或是透過給button綁定window.location透過JS跳到PHP) 。這種做法就得為每一個button設計一個PHP程式碼。

而我的做法是:事件 ajax 給予button綁定點擊事件,然後執行ajax。

1、server.php

<?php 
	if (isset($_POST[&#39;action&#39;]))
	{
		switch($_POST[&#39;action&#39;])
		{
			case "btn1":btn1();break;
			case "btn2":btn2();break;
			default:break;
		}
	}
	
	function btn1()
	{
		echo "hello 按钮1";
	}
	function btn2()
	{
		echo "hello 按钮2";
	}
 
?>
登入後複製

#方式一:index.php

<html>
<head>
	<style>
	div {width:600px;margin:200px auto;}
	
	.btn {
		background-color:#44c767;
		-moz-border-radius:28px;
		-webkit-border-radius:28px;
		border-radius:28px;
		border:1px solid #18ab29;
		display:inline-block;
		cursor:pointer;
		color:#ffffff;
		font-family:Arial;
		font-size:17px;
		padding:16px 31px;
		text-decoration:none;
		text-shadow:0px 1px 0px #2f6627;
	}
	.btn:hover {
		background-color:#5cbf2a;
	}
	.btn:active {
		position:relative;
		top:1px;
	}
	
	#btn2 {float:right;}
	
	</style>
	
	<script type="text/javascript" language="javascript" src="jquery.js"></script>
	<script type="text/javascript" language="javascript">
		function fun(n) {
			$.ajax({
				url:"server.php", 			//the page containing php script
				type: "POST", 				//request type
				data:{action: n.value},
				success:function(result){
					alert(result);
				}
			});
		}
		
		function fun2(n) {
			var url = "server.php";
			var data = {
				action : n.value
			};
			jQuery.post(url, data, callback);
		}
		function callback(data) {
			alert(data);
		}
	</script>
</head>
 
<body>
	<div>
		<button type="button" class="btn" id="btn1" οnclick="fun(this)"  value="btn1">按钮1</button>
		<button type="button" class="btn" id="btn2" οnclick="fun2(this)" value="btn2">按钮2</button>
	</div>
</body>
 
 
</html>
登入後複製


##。 ##方式二:index.php

<html>
<head>
	<style>
		div {width:600px;margin:200px auto;}
		
		.btn {
			background-color:#44c767;
			-moz-border-radius:28px;
			-webkit-border-radius:28px;
			border-radius:28px;
			border:1px solid #18ab29;
			display:inline-block;
			cursor:pointer;
			color:#ffffff;
			font-family:Arial;
			font-size:17px;
			padding:16px 31px;
			text-decoration:none;
			text-shadow:0px 1px 0px #2f6627;
		}
		.btn:hover {
			background-color:#5cbf2a;
		}
		.btn:active {
			position:relative;
			top:1px;
		}
		
		#btn2 {float:right;}
	</style>
	<script type="text/javascript" language="javascript">
		var xmlHttp;  
		function createXMLHttpRequest(){ 
			//检查浏览器是否支持 XMLHttpRequest 对象
			if(window.ActiveXObject){  
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
			}  
			else if(window.XMLHttpRequest){  
				xmlHttp = new XMLHttpRequest();  
			}  
		}  
		function fun(n){  
			createXMLHttpRequest();  
			var url="server.php";  
			xmlHttp.open("POST",url,true); 
			xmlHttp.setRequestHeader(&#39;Content-type&#39;, &#39;application/x-www-form-urlencoded&#39;);
			xmlHttp.onreadystatechange = callback;  
			xmlHttp.send("action=" + n.value);  
		}  
		function callback(){  
			if(xmlHttp.readyState == 4){  
				if(xmlHttp.status == 200){  
					alert(xmlHttp.responseText);   
				}  
			}  
		} 
	</script>
</head>
 
<body>
	<div>
		<button type="button" class="btn" id="btn1" οnclick="fun(this)" value="btn1">按钮1</button>
		<button type="button" class="btn" id="btn2" οnclick="fun(this)" value="btn2">按钮2</button>
	</div>
</body>
 
 
</html>
登入後複製
方式一參考資料:

jQuery ajax - post() 方法:

#http://www.w3school.com.cn/jquery/ajax_post.asp

方式二參考資料:

建立XMLHttpRequest物件:

http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_create.asp

向伺服器發送請求:

#http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_send.asp

######伺服器回應:#########http://www.w3school.com.cn /ajax/ajax_xmlhttprequest_response.asp#########onreadystatechange 事件:#########http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_onreadystatechange.asp##### ###更有相關問題請上PHP中文網:###PHP影片教學######

以上是PHP對應button中的onclick事件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!