留言板------Ajax与mysql数据交互01

最近自己做了一个小demo,实现了Ajax与mysql的数据交互,js部分用的是jq,后台用的是php,数据库是mysql,过时间再来一个node+mongodb版的。

关于mysql的使用和安装,就不多讲了,自行百度xampp,Apache服务器和mysql数据库集成,非常好用

首先打开服务器和数据库,我这里先建立了一个“eleven”的数据库,下面建立了一个叫做microblog的表(请注意:我这里使用的是高版本的mysql,里面php链接数据库的方法使用的都是mysqli_  如果版本过低,请使用mysql_ 方法,自行修改代码)

以下是代码部分:

html页面和js部分:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>微博留言板</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			#box{
				width: 600px;
				/*height: 500px;*/
				border: 2px solid rgb(85,85,85);
				border-radius: 15px;
				margin: 50px auto;
				padding: 20px 10px 15px;
				background-color: rgb(85,85,85);
			}
			#content{
				display: block;
				resize: none;
				width: 550px;
				height: 200px;
				margin: 0 auto;
				border: 2px solid rgb(225,225,225);
				border-radius: 10px;
				text-align: center;
				font-size: 30px;
				background-color: rgb(225,225,225);
			}
			#content:focus{
				outline: none;
				border: 2px solid rgb(225,225,225);
				box-shadow: 0 0 15px rgb(225,225,225);
			}
			#btn{
				border: 2px solid rgb(255,204,0);
				width: 80px;
				height: 40px;
				border-radius: 5px;
				margin-top: 30px;
				font-size: 17px;
				cursor: pointer;
				outline: none;
				background-color: rgb(255,204,0);
			}
			
			.list{
				list-style: none;
				background-color: rgb(249,249,249);
				margin-top: 20px;
			}
			.list>li{
				padding: 20px 10px 10px;
				border-bottom: 2px solid rgb(68,68,68);
				font-size: 20px;
				color: rgb(200,214,225);
				position: relative;
				word-break: break-word;
				word-wrap: break-word;
				background-color: rgb(85,85,85);
				
			}
			.list>li>.control{
				position: absolute;
				bottom: 3px;
				right: 5px;
				font-size: 14px;
			}
			.list>li>p{
				margin-bottom: 25px;
			}
			.control span,.control em{
				display: inline-block;
				margin-right: 15px;
			}
			.control em{
				color: darkblue;
				cursor: pointer;
			}
			a{
				text-decoration: none;
				color: darkred;
			}
			#page>a{
				display:inline-block;
				width: 40px;
				height: 30px;
				margin-top: 10px;
				text-align: center;
				line-height: 30px;
				font-size: 20px;
				border-radius: 5px;
				color: white;
				background-color: rgb(51,21,70);
			}
			#head{
				color: rgb(200,214,225);
				font-size: 30px;
				height: 50px;
				border-bottom: 2px solid rgb(68,68,68);
				margin-bottom: 20px;
			}
		</style>
	</head>
	<body>
		<div id="box">
			<div id="head">
				留言板
			</div>
			<div id="fill_in">
				<textarea id="content"></textarea>
				<button id="btn">提交留言</button>
			</div>
			<!--留言列表-->
			<div id="message_text">
				<ul class="list">
				</ul>
			</div>
			<!--分页-->
			<div id="page">
				<a href="javasript:void(0)">1</a>
				<a href="javasript:void(0)">2</a>
			</div>
		</div>
	</body>
	<script src="Jq/jquery-3.1.1.min.js">
</html>

代码显示不完,下一篇是php部分代码,请原谅分两篇!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对初学AJAX的同学会有帮助的。 ----------ajax.js--------------- var request = false; try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; } } } if (!request) alert("Error initializing XMLHttpRequest!"); function getCustomerInfo() { document.getElementById("output").innerHTML = "Loading..."; document.getElementById("submit").disabled = true; var name = document.getElementById("name").value; var text = document.getElementById("text").value; var key = document.getElementById("key").value; var answer = document.getElementById("answer").value; var poststr = "name="+name+"&text="+text+"&key="+key+"&answer="+answer; var url = "doit.php"; request.open("POST", url, true); request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); request.send(poststr); request.onreadystatechange = updatePage; } function updatePage() { if (request.readyState == 4) if (request.status == 200) document.getElementById("output").innerHTML = request.responseText; else if (request.status == 404) alert("目标地址不存在"); else alert("警告: 错误代码 " + request.status); } function getInfo() { document.getElementById("output1").innerHTML = "Loading..."; var url = "show.php"; var d = new Date().getTime(); request.open("GET", url+"?d="+d.toString(), true); request.onreadystatechange = updatePage1; request.send(); } function updatePage1() { if (request.readyState == 4) if (request.status == 200) document.getElementById("output1").innerHTML = request.responseText; else if (request.status == 404) alert("目标地址不存在"); else alert("警告: 错误代码 " + request.status); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值