1个回答
展开全部
我这里有个类似的例子:
数据库配置信息:
<?php
//数据库配置信息(用户名,密码,数据库名,表前缀等)
$cfg_dbhost = "localhost";
$cfg_dbuser = "root";
$cfg_dbpwd = "1";
$cfg_dbname = "test";
$cfg_dbprefix = "";
$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
mysql_query("set names utf8");
?>
接收数据:
<?php
header("Content-type:text/html;charset=utf-8");
include "config.php";
//post接收数据,只是演示效果,这里就省去验证了
$name = $_POST['name'];
$content = $_POST['content'];
$sql = "insert into test (name,content) values ('{$name}','{$content}');";
$res = mysql_query($sql,$link);
if($res){
echo '{"name": "'.$name.'","content": "'.$content.'","status": "1"}';
}
?>
ajax+jquery的刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无刷新</title>
<link href="css/css.css" type="text/css" rel="stylesheet" />
<style type="text/css">
body {
color: #555;
font-size: 14px;
padding: 0;
margin: 0;
}
#form {
background: #dedede;
padding: 10px 20px;
width: 300px;
}
#show {
background: #f6f6f6;
padding: 10px 20px;
width: 300px;
}
#show p {
margin: 6px;
font-size: 13px;
line-height: 22px;
border-bottom: 1px dashed #cdcdcd;
}
</style>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#sub").click(function(){
//只是说明原理,然后这里省去了验证文本框内容的步骤,直接发送ajax请求
$.post("deal.php",{name : $("#name").val(), content : $("#content").val()}, function(data){
if(data.status){
var str = "<p><strong>"+data.name+"</strong> 发表了:"+data.content+"</p>";
$("#show").prepend(str); //在前面追加
}else{
alert("评论失败");
}
}, 'json');
});
});
</script>
</head>
<body>
<div id="form">
<form action="deal.php" method="get" id="suggest_form">
用户名:<input type="text" name="name" id="name" /><br />
内 容:<textarea name="content" id="content">
</textarea> <input type="button" value="发布" id="sub" />
</form>
</div>
<div id="show">
<?php
include "config.php";
$sql = "select * from test;";
$res = mysql_query($sql,$link);
while($row=mysql_fetch_array($res)){
echo "<p><strong>".$row['name']."</strong> 发表了:".$row['content']."</p>";
}
?>
</div>
</body>
</html>
数据库配置信息:
<?php
//数据库配置信息(用户名,密码,数据库名,表前缀等)
$cfg_dbhost = "localhost";
$cfg_dbuser = "root";
$cfg_dbpwd = "1";
$cfg_dbname = "test";
$cfg_dbprefix = "";
$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
mysql_query("set names utf8");
?>
接收数据:
<?php
header("Content-type:text/html;charset=utf-8");
include "config.php";
//post接收数据,只是演示效果,这里就省去验证了
$name = $_POST['name'];
$content = $_POST['content'];
$sql = "insert into test (name,content) values ('{$name}','{$content}');";
$res = mysql_query($sql,$link);
if($res){
echo '{"name": "'.$name.'","content": "'.$content.'","status": "1"}';
}
?>
ajax+jquery的刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无刷新</title>
<link href="css/css.css" type="text/css" rel="stylesheet" />
<style type="text/css">
body {
color: #555;
font-size: 14px;
padding: 0;
margin: 0;
}
#form {
background: #dedede;
padding: 10px 20px;
width: 300px;
}
#show {
background: #f6f6f6;
padding: 10px 20px;
width: 300px;
}
#show p {
margin: 6px;
font-size: 13px;
line-height: 22px;
border-bottom: 1px dashed #cdcdcd;
}
</style>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#sub").click(function(){
//只是说明原理,然后这里省去了验证文本框内容的步骤,直接发送ajax请求
$.post("deal.php",{name : $("#name").val(), content : $("#content").val()}, function(data){
if(data.status){
var str = "<p><strong>"+data.name+"</strong> 发表了:"+data.content+"</p>";
$("#show").prepend(str); //在前面追加
}else{
alert("评论失败");
}
}, 'json');
});
});
</script>
</head>
<body>
<div id="form">
<form action="deal.php" method="get" id="suggest_form">
用户名:<input type="text" name="name" id="name" /><br />
内 容:<textarea name="content" id="content">
</textarea> <input type="button" value="发布" id="sub" />
</form>
</div>
<div id="show">
<?php
include "config.php";
$sql = "select * from test;";
$res = mysql_query($sql,$link);
while($row=mysql_fetch_array($res)){
echo "<p><strong>".$row['name']."</strong> 发表了:".$row['content']."</p>";
}
?>
</div>
</body>
</html>
追问
config.php和deal.php的代码在哪里呀
追答
前两个的名字啊,第一个为config.php,第二个deal.php
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询