Ajax+php 无刷新更新数据.并将数据库操作改写成类.

Ajax+php 无刷新更新数据.并将数据库操作改写成类.

Ajax+php 无刷新更新数据.

数据库建表

sql.sql

- phpMyAdmin SQL Dump
-- version 2.11.1.2
-- http://www.phpmyadmin.net
--
-- 主机: 137.200.32.183
-- 生成日期: 2009 年 07 月 01 日 13:35
-- 服务器版本: 5.0.51
-- PHP 版本: 5.2.4

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `test`
--

-- --------------------------------------------------------

--
-- 表的结构 `cr_fourm`
--

CREATE TABLE IF NOT EXISTS `cr_fourm` (
`id` int(11) NOT NULL,
`username` varchar(20) character set utf8 collate utf8_unicode_ci NOT NULL,
`newfourm` varchar(30) character set utf8 collate utf8_unicode_ci NOT NULL,
`time` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- 导出表中的数据 `cr_fourm`
--

INSERT INTO `cr_fourm` (`id`, `username`, `newfourm`, `time`) VALUES
(1, '2', '3', '2009-08-21'),
(1, '2', '3', '2009-08-21'),
(1, '2', '3', '2009-08-21'),
(1, '3', '1', '0000-00-00'),
(1, '545', '4', '0000-00-00'),
(1, '232', '2323', '0000-00-00'),
(1, '2008', '2009', '0000-00-00'),
(2, 'good', 'well', '2009-07-15'),
(1, '2008', '2009', '0000-00-00'),
(1, 'china', 'shanghai', '2009-07-01'),
(1, '??????', '???', '2009-07-01'),
(1, '河南郑州', '???', '2009-07-01'),
(1, '评论', '发表', '2009-07-01');

up.php

************************************************************************************************

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript" src="ajax.js"></script>
</HEAD>

<BODY>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center"></td>
      </tr>
      <tr>
        <td align="center">
</td>
      </tr>
      <tr>
        <td align="center"><br><br>
<div align="center" id="result"></div>
<form name="fourm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="25"> 快速发表评论<span class="STYLE1">(必须先登陆)<BR>用户名:
            <input name="username" type="text" value="">
            </span></td>
          </tr>
        
    <tr>
            <td height="32" align="" valign="middle">
    <textarea name="newfourm" class="f" id="newfourm"></textarea>
    </td>
          </tr>
         
    <tr>
            <td height="32"> <input name="submit" type="button" value="发表评论" onClick=checkfourm("result")>
              <input name="reset" type="reset" id="reset" value="重新填写">
            <input name="id" type="hidden" id="id" value="<?php echo"$id";?>"></td>
          </tr>
        </table>
        </form>
        </td>
      </tr>
    </table>

</BODY>
</HTML>

ajax.php

*****************************************************************************

var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
    http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
    http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE浏览器
try{
   http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
   try{
   http_request=new ActiveXobject("Microsoft.XMLHttp");
   }catch(e){}
}
    }
if(!http_request){//异常,创建对象实例失败
window.alert("创建XMLHttp对象失败!");
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
    http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
   function processrequest(){
   if(http_request.readyState==4){//判断对象状态
     if(http_request.status==200){//信息已成功返回,开始处理信息
   document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//页面不正常
   alert("您所请求的页面不正常!");
}
   }
}
   function checkfourm(obj){
    var f=document.fourm;
    var newfourm=f.newfourm.value;
    var username=f.username.value;
    var id=f.id.value;
    if(username==""){
           document.getElementById(obj).innerHTML="<img src=images/false.gif> <font color=red>您必须先登录!</font>";
     return false;
    }
    else if(newfourm==""){
     document.getElementById(obj).innerHTML=" <font color=red>您还没填写评论内容!</font>";
     return false;
    }
    else{
     document.getElementById(obj).innerHTML="正在发送数据...";
     send_request("sendnewfourm.php?username="+username+"&newfourm="+newfourm+"&id="+id);
     reobj=obj;
    }
   }

sendnewfourm1.php

*****************************************************************************************************************

<?php
header("Content-Type:text/html;charset=GB2312");//避免输出中文乱码,linux下不需要
$username=trim($_GET["username"]);
$newfourm=trim($_GET["newfourm"]);
$id=$_GET["id"];
$time=date("Y-m-d");
  

$con = mysql_connect('137.200.32.183', 'root', '123456');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("SET NAMES GB2312");

mysql_select_db("test", $con);

$sql="insert into cr_fourm(id,username,newfourm,time) values('1','$newfourm','$username','$time')";
echo $newfourm;
echo $username;
echo $time;
$result = mysql_query($sql);
echo"<font color=red>评论已成功发表!</font>";

?>

将数据操作写成一个类dbclasss.php ,数据连接参数单独定义在config.inc.php

<?php
//定义数据库操作类
class db{
//类属性定义
var $dbhost="137.200.32.183";//MYSQL主机
var $dbuser="root";//连接帐户
var $password="";//连接密码
var $dbname="";//数据库名
//变量引用
function mysql($dbhost,$dbuser,$password,$dbname){
$this->dbhost=$dbhost;
$this->dbuser=$dbuser;
$this->password=$password;
$this->dbname=$dbname;
}
//创建MYSQL连接
function mycon(){
@mysql_connect($this->dbhost,$this->dbuser,$this->password);
}
//选择相应的数据库
function selectdb(){
@mysql_select_db($this->db);
}
//创建数据库连接并选择相应数据库
function createcon(){
mysql_connect($this->dbhost,$this->dbuser,$this->password);
mysql_query("SET NAMES 'GB2312'");//这是解决乱码的关键哦,LINUX下改为UTF8
mysql_select_db($this->dbname);
}
//执行SQL语句,并返回结果集
function fetch_array($sql){
$result=mysql_query($sql);
return mysql_fetch_array($result);
}
//执行SQL语句
function query($sql){
return mysql_query($sql);
}
//取得结果集数组
function loop_query($result){
return mysql_fetch_array($result);
}
//关闭数据库连接
function close() {
return mysql_close();
}
}
?>

config.inc.php

*******************************************

<?php

/*
* Created on 2007-11-10
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
// database host
//$dbhost = "localhost";
$dbhost = "137.200.32.183";

// database name
$dbname = "test";

// database username
$dbuser = "root";

// database password
$password = "123456";

?>

原文地址:https://www.cnblogs.com/visi_zhangyang/p/2621997.html