php中post请求的写法

<!DOCTYPE html>
<html lang="en">

<head>
          <meta charset="UTF-8">
          <title>Document</title>
          <style type="text/css">
          h1 {
                    text-align: center;
          }
          
          .showBox {
                    width: 200px;
                    height: 200px;
                    border: 1px solid #0094ff;
                    margin: 100px auto 20px;
          }
          
          .btnBox {
                    text-align: center;
          }
          
          .btnBox input {
                    margin: 0px 20px;
                    border-width: 0px;
                    height: 20px;
                    width: 80px;
                    background: skyblue;
                    color: white;
          }
          </style>
</head>

<body>
          <h1>ajax切换明星头像</h1>
          <div class='showBox'>
          </div>
          <div class="btnBox">
                    <input type="button" class='starBtn' value='海贼王'  data-star='onePiece'>
                    <input type="button" class='starBtn' value='李彦宏' data-star='lyh'>
                    <input type="button" class='starBtn' value='马云' data-star='my'>
          </div>
</body>

</html>
<script type="text/javascript">


// 返回的是 数组
var btns = document.querySelectorAll('.starBtn');

// 循环绑定点击事件
for (var i = 0; i < btns.length; i++) {
          // 绑定点击事件
          btns[i].onclick = function(event) {
          	//1 创建
          	var xmlOBj = new XMLHttpRequest();

          	//2 设置
          	xmlOBj.open('post','changeStar.php');

          	xmlOBj.setRequestHeader("Content-type","application/x-www-form-urlencoded");

          	//3 发送
          	//获取 自定义的 data-star属性 this 就是 当前 按钮
	// console.log(this.dataset['star']);      	

	// 拼接字符串
          	xmlOBj.send('starName='+this.dataset['star']);

          	// 4.绑定事件
          	xmlOBj.onreadystatechange = function () {
          		if (xmlOBj.readyState == 4 && xmlOBj.status == 200) {
          			//数据才是正确的
          			// xmlOBj.responseText;
          			console.log(xmlOBj.responseText);

          			// 更新页面
          			// 获取元素  设置 background属性即可
          			document.querySelector('.showBox').style.background = "url("+xmlOBj.responseText+") no-repeat center/200px 200px";
          		}
          	}
          }
}
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值