分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

用原生JS&PHP简单的AJAX实例

发布时间:2023-09-06 01:59责任编辑:蔡小小关键词:PHP

功能介绍:

1)file.html 使用 xmlhttp 请求服务器端文件 text ,并更新 file.html 的部分内容

2)update.html 使用 xmlhttp 通过 filewrite.php 更改服务器端文件 text 的内容

3)filewrite.php 接收 update.html 中 xmlhttp 提交的数据更新 text 内容

file.html

<!DOCTYPE html><html> ???<head> ???????<meta charset=‘utf-8‘> ???????<title>ajax</title> ???????<style> ???????????body { ???????????????background:whitesmoke; ???????????} ???????????* { ???????????????margin: 0 0 0 0; ???????????????padding: 0 0 0 0; ???????????} ???????????.header { ???????????????height:100px; ???????????????width:100%; ???????????????text-align: center; ???????????????font-size: 80px; ???????????????color:gray; ???????????} ???????????.container { ???????????????text-align: center; ???????????} ???????????.ipttag { ???????????????display: inline-block; ???????????????width:100px; ???????????????color: brown; ???????????} ???????????????????????#responseContent { ???????????????color : rgb(20, 104, 214); ???????????} ???????</style> ???????????</head> ???<body> ???????<div class="header">FILE Learning</div> ???????<div class="container"> ???????????<form> ???????????????<input type=‘button‘ value=‘获取text内容‘ id="submit"/> ???????????????????????????????<p id="responseContent"></p> ???????????</form> ???????</div> ???</body> ???<script> ????????????document.getElementById(‘submit‘).onclick = ?function() { ???????????????//id = document.getElementById(‘id‘).value; ???????????????//passwd = document.getElementById(‘passwd‘).value; ???????????????var xmlhttp; ???????????????xmlhttp = new XMLHttpRequest(); ???????????????xmlhttp.onreadystatechange = function() { ???????????????????if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) ???????????????????{ ???????????????????????//alert(xmlhttp.responseText); ???????????????????????document.getElementById(‘responseContent‘).innerHTML = xmlhttp.responseText; ???????????????????} ???????????????} ???????????????xmlhttp.open("GET", "./text", true); ???????????????xmlhttp.send(); ???????????} ???????????//document.getElementById(‘submit‘).onclick = showHints; ???????????//document.getElementById(‘id‘).onkeyup = showHints; ???????</script></html>

update.html

<!DOCTYPE html><html> ???<head> ???????<meta charset=‘utf-8‘> ???????<title>UPDATE</title> ???????<style> ???????????????body { ???????????????????background:whitesmoke; ???????????????} ???????????????* { ???????????????????margin: 0 0 0 0; ???????????????????padding: 0 0 0 0; ???????????????} ???????????????.header { ???????????????????height:100px; ???????????????????width:100%; ???????????????????text-align: center; ???????????????????font-size: 80px; ???????????????????color:gray; ???????????????} ???????????????.container { ???????????????????text-align: center; ???????????????} ???????????????????????????????#responseContent { ???????????????????color : rgb(20, 104, 214); ???????????????} ???????????????.text { ???????????????????width : 500px; ???????????????????height: 300px; ???????????????????text-align: left; ???????????????} ???????????</style> ???????</head> ???<body> ???????<div class=‘header‘>FILE Learing</div> ???????<div class=‘container‘> ???????????<textarea class=‘text‘ type="area" id=‘text‘></textarea> ???????????<br> ???????????<input type=‘button‘ value=‘更新text内容‘ id=‘updateButton‘> ???????????<p id="responseContent"></p> ???????</div> ???????????</body> ???<script> ???????????????document.getElementById(‘updateButton‘).onclick = function () ???????{ ???????????var text = document.getElementById(‘text‘).value; ???????????var postContent = ‘text=‘ + text; ???????????var xmlhttp = new XMLHttpRequest(); ???????????xmlhttp.onreadystatechange = function () { ???????????????if (xmlhttp.readyState == 4 && xmlhttp.status == 200) ???????????????{ ???????????????????//document.getElementById(‘responseContent‘).innerHTML = "更新成功"; ???????????????????alert(‘更新成功‘); ???????????????} ???????????} ???????????xmlhttp.open(‘POST‘, ‘./filewrite.php‘, true); ???????????xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ???????????xmlhttp.send(postContent); ???????} ???</script></html>

filewrite.php

<?php$text = $_POST[‘text‘];$file = fopen(‘./text‘, ‘w‘) or exit(‘无法打开文件‘);fwrite($file, $text);fclose($file);?>

用原生JS&PHP简单的AJAX实例

原文地址:https://www.cnblogs.com/liupy/p/9165227.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved