php實現button的onclick事件的方法

本篇文章為大家展示了php實現button的onclick事件的方法,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

我們提供的服務有:成都做網站、網站建設、微信公眾號開發(fā)、網站優(yōu)化、網站認證、杏花嶺ssl等。為超過千家企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的杏花嶺網站制作公司

PHP是服務器端代碼 ,html是客戶端代碼,實現button的onclick事件,就是客戶端調用服務器端函數,因此就得向服務器提交請求。

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

而我的做法是:事件+ajax 給button綁定點擊事件,然后執(zhí)行ajax。

示例:

server.php

<?php 
	if (isset($_POST['action']))
	{
		switch($_POST['action'])
		{
			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>

上述內容就是php實現button的onclick事件的方法,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道。

網頁題目:php實現button的onclick事件的方法
網站路徑:http://bm7419.com/article30/jdghso.html

成都網站建設公司_創(chuàng)新互聯,為您提供移動網站建設面包屑導航、全網營銷推廣、網站導航、網站內鏈、Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

商城網站建設