博客列表 >1. 实例演示Ajax的get,post请求; 2. 练习选顶卡和换肤案例

1. 实例演示Ajax的get,post请求; 2. 练习选顶卡和换肤案例

小丑0o鱼
小丑0o鱼原创
2021年07月20日 17:53:55508浏览
  1. <?php
  2. $users = [
  3. [‘id’=>1, name’=>’Peter’, email’=>’peter@abc.com’, password’=> md5(‘123456’)],
  4. [‘id’=>2, name’=>’Joe’, email’=>’joe@abc.com’,’password’=> md5(‘abc123’)],
  5. [‘id’=>3, name’=>’Mary’,’email’=>’mary@abc.com’,’password => md5(‘abc456’)],
  6. ];
  7. $id = $_GET[‘id’];
  8. $key = array_search($id,array_column($users.’id’));
  9. echo json_encode($users[$key]);

配置:输入请求的脚本和查询条件代替url

  1. xhr.open(type,”test1.php?id=2”);
  2. 处理 xhr 响应
  3. xhr.onload = () => {
  4. console.log(xhr.response);};
  5. xhr.onerror = () => console.log(object);
  6. 发送 xhr 请求
  7. xhr.send(null)
  8. <button>Ajax.Get request</button>
  1. <script>
  2. const btn = document.querySelector(“button);
  3. btn.onclick = () => {
  4. const xhr = new XMLHttpRequest();
  5. xhr.open(type,”test1.php?id=2”);
  6. }
  7. </script>

2. Ajax的get请求

  • 创建一个表单,selector
  1. </head>
  2. <body>
  3. <div class="login">
  4. <p>Login Name</p>
  5. <form action="" onsubmit="return false">
  6. <label for="email">email:</label>
  7. <input type="email" name="email" id="email" placeholder="demo@mail.com" />
  8. <label form="password">Password</label>
  9. <input type="password" name="password" id="password" placeholder="minimum 6 " />
  10. <button>Submit</button>
  11. <span class="tips"></span>
  12. </form>
  13. <script>
  14. const form = document.querySelector(“.login form”);
  15. const btn = document.querySelector(“.login button”);
  16. const tips = document.querySelector(“.tips”);
  17. - #### Ajax post xhr四步骤
  18. btn.onclick = (ev) => {
  19. ev.preventDefault();
  20. const xhr = new XMLHttpRequest();
  21. xhr.open(“post”, “test2.php”);
  22. xhr.onload = ( => {
  23. console.log(xhr.response);
  24. })
  25. xhr.onerrer = () => console.log(“Error”);
  26. xhr:send(new FormData(form);
  27. ### 2. 选顶卡
  28. - 导航tabs,items
  29. <div class="tabs">
  30. <ul class="tab">
  31. <li class="active" data-index="1">Handbags</li>
  32. <li data-index="2">Wallets</li>
  33. <li data-index="3">Shoes</li>
  34. </ul>
  35. <ul data-index="1" class="item active">
  36. <li><a href="">tote bags</a></li>
  37. <li><a href="">crossbody bags</a></li>
  38. <li><a href="">belt bags</a></li>
  39. <li><a href="">mens bags</a></li>
  40. </ul>
  41. <ul data-index="2" class="item">
  42. <li><a href="">clutch long wallets</a></li>
  43. <li><a href="">card holders</a></li>
  44. <li><a href="">short wallets</a></li>
  45. <li><a href="">key pouches</a></li>
  46. </ul>
  47. <ul data-index="3" class="item">
  48. <li><a href="">fashion sneakers </a></li>
  49. <li><a href="">fashion sandals </a></li>
  50. <li><a href="">slids</a></li>
  51. <li><a href="">running shoes</a></li> </ul>
  52. </div>
  1. <script>
  2. const tab = document.querySelector(“.tab”);
  3. const items = document.querySelectorAll(“.item”);
  4. tab.onclick = (ev) => {
  5. […tab.children].forEach((item) => item.classList.remove(“active”));
  6. ev.target.classList.add(“active”);
  7. items.forEach((item) => item.classList.remove(“active”));
  8. […items]
  9. .filter((item) => item.dataset.index === ev.target.dataset.index)
  10. .pop()
  11. .classList.add(“active”);
  12. };
  13. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议