首页 > 编程技术 > php

PHP+ajax实现可以编辑的单元格点击可以修改

发布时间:2016-11-25 17:29

本文章来给大家介绍一个PHP+ajax实现可以编辑的单元格点击可以修改实例效果代码,有需了解的同学可进入参考。

数据库结构

 代码如下 复制代码

-- phpMyAdmin SQL Dump
-- version 2.11.2-rc1
-- http://www.111cn.net
--
-- 主机: localhost
-- 生成日期: 2010 年 11 月 09 日 05:34
-- 服务器版本: 5.0.41
-- PHP 版本: 5.2.4

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `demo`
--

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

--
-- 表的结构 `customer`
--

CREATE TABLE IF NOT EXISTS `customer` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
  `solutation` varchar(40) NOT NULL,
  `phone` varchar(50) NOT NULL,
  `company` varchar(100) NOT NULL,
  `mobile` varchar(50) NOT NULL,
  `source` varchar(50) NOT NULL,
  `sdate` date NOT NULL,
  `job` varchar(50) NOT NULL,
  `web` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `createtime` datetime NOT NULL,
  `modifiedtime` datetime default NULL,
  `note` varchar(500) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

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

INSERT INTO `customer` (`id`, `username`, `solutation`, `phone`, `company`, `mobile`, `source`, `sdate`, `job`, `web`, `email`, `createtime`, `modifiedtime`, `note`) VALUES
(1, '李小三', '先生', '(513) 378-6268', '常丰集团', '13800138000', '合作伙伴', '2011-11-30', '部门经理', 'www.helloweba.com', 'lrfbeyond@163.com', '2010-11-04 21:11:59', '2010-11-08 09:30:35', '备注信息n');


php代码

 代码如下 复制代码

<?php
include_once("connect.php");
$query=mysql_query("select * from customer where id=1");
$rs=mysql_fetch_array($query);
$username=$rs['username'];
$userid=$rs['userid'];
$solutation=$rs['solutation'];
$phone=$rs['phone'];
$company=$rs['company'];
$mobile=$rs['mobile'];
$source=$rs['source'];
$sdate=$rs['sdate'];
$job=$rs['job'];
$web=$rs['web'];
$email=$rs['email'];
$createtime=$rs['createtime'];
$modifiedtime=$rs['modifiedtime'];
$note=$rs['note'];
?>
<!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>可编辑的表格:jQuery+PHP实现实时编辑表格字段内容-Helloweba演示平台</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<style type="text/css">
table{width:96%; margin:20px auto; border-collapse:collapse;}
table td{line-height:26px; padding:2px; padding-left:8px; border:1px solid #b6d6e6;}
.table_title{height:26px; line-height:26px; background:url(css/btn_bg.gif) repeat-x bottom; font-weight:bold; text-indent:.3em; outline:0;}
.table_label{background:#e8f5fe; text-align:right; }
.btn{height:24px; background:url(css/btn_bg.gif) repeat-x; padding:0 4px; font-size:12px; border:1px solid #ccc; cursor:pointer; margin-left:3px}
.input{border:1px solid #369; background:#ffc; padding:2px}
.msg{padding-left:30px; margin-top:20px}
textarea{font-size:12px}
</style>
<script type="text/javascript"" width=100% src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"" width=100% src="js/jquery.jeditable.js"></script>
<script type='text/javascript'" width=100% src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js'></script>
<script type="text/javascript">
$(function(){
$('.edit').editable('save.php');
$('.edit_select').editable('save.php', {
loadurl : 'json.php',
type : "select",
style : 'display: inline'
});

$(".edit_email").editable('save.php', {
type : 'email',
select : true
});
$(".edit_mobile").editable('save.php', {
type : 'mobile',
select : true
});
$(".edit_web").editable('save.php', {
type : 'url',
select : true
});
});
</script>
</head>

<body>
<div id="main">
<h2 class="top_title"><a href="">可编辑的表格:jQuery+PHP实现实时编辑表格字段内容</a></h2>
<div class="msg"><strong>提示</strong>:点击表格中字段对应的内容即可进行在线编辑。</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr class="table_title">
<td colspan="4"><span class="open"></span>客户信息</td>
</tr>
</thead>
<tbody>
<tr>
<td width="20%" class="table_label">姓名</td>
<td width="30%" class="edit" id="username"><?php echo $username; ?></td>
<td width="20%" class="table_label">办公电话</td>
<td width="30%" class="edit" id="phone"><?php echo $phone; ?></td>
</tr>
<tr>
<td class="table_label">称谓</td>
<td class="edit" id="solutation"><?php echo $solutation; ?></td>
<td class="table_label">手机</td>
<td class="edit" id="mobile"><?php echo $mobile; ?></td>
</tr>
<tr>
<td class="table_label">公司名称</td>
<td class="edit" id="company"><?php echo $company; ?></td>
<td class="table_label">电子邮箱</td>
<td class="edit" id="email"><?php echo $email; ?></td>
</tr>
<tr>
<td class="table_label">潜在客户来源</td>
<td class="edit_select" id="source"><?php echo $source; ?></td>
<td class="table_label">网站</td>
<td class="edit" id="web"><?php echo $web; ?></td>
</tr>
<tr>
<td class="table_label">职位</td>
<td class="edit" id="job"><?php echo $job; ?></td>
<td class="table_label">修改时间</td>
<td id="modifiedtime"><?php echo $modifiedtime; ?></td>
</tr>
</tbody>
</table>
</div>


</body>
</html>

sava.php代码

 代码如下 复制代码

<?php
include_once("connect.php");

$field=$_POST['id'];

$val=$_POST['value'];
$val = htmlspecialchars($val, ENT_QUOTES);
if($field=="note"){
 if(strlen($val)>100){
  echo "您输入的字符大于100字了";
  exit;
 }
}
$time=date("Y-m-d H:i:s");
if(empty($val)){
    echo "不能为空";
}else{
 $query=mysql_query("update customer set $field='$val',modifiedtime='$time' where id=1");
 if($query){
    echo $val;
 }else{
    echo "数据出错"; 
 }
}
?>


例下载地址:http://file.111cn.net/download/2013/05/16/3788_72817.rar

本文章来给大家介绍在PHP取网页所有链接实现程序代码,有直接利用正则的也有用在采集中过滤所有连接地址,有需要了解的朋友可参考。

我们核心代码就是正则表达试了

 代码如下 复制代码

function get_all_url($code){
             preg_match_all(‘/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i’,$code,$arr);
            return array(‘name’=>$arr[2],’url’=>$arr[1]);
}

上面只是简单的下面看全例

 代码如下 复制代码

$url ='http://www.111cn.net';
$body=@file_get_contents($url);
preg_match_all('/href=['"]?([^'"]*)['"]?>(.*)/i',$body,$b);
$nums = array();
foreach($b[1] as $u){
  if(in_array($u,$nums)){
  continue;
  }
  $nums[]=$u;
  $title=strip_tags($u);
  echo $title."</br>";
}

利用curl函数来获取

 代码如下 复制代码

<?php
/*
 * 使用curl 采集www.111cn.net下的所有链接。 
 */
include_once('function.php');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.111cn.net/');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
 echo "cURL Error: " . curl_error($ch);
}

curl_close($ch);
$linkarr = _striplinks($html);
// 主机部分,补全用
$host = 'http://www.111cn.net/';
if (is_array($linkarr)) {
 foreach ($linkarr as $k => $v) {
  $linkresult[$k] = _expandlinks($v, $host);
 }
}

printf("<p>此页面的所有链接为:</p><pre>%s</pre>n", var_export($linkresult , true));

?>

获取所有网址,但只有不重复的数据

 代码如下 复制代码

<?
  $e=clinchgeturl("http://im286.com/forumdisplay.php?fid=1");
  
  var_dump($e);
  function clinchgeturl($url)
  {
  
  //$url="http://127.0.0.1/1.htm";
  //$rootpath="http://fsrootpathfsfsf/yyyyyy/";
  //var_dump($rrr);
  if(eregi('(.)*[.](.)*',$url)){
   $roopath=split("/",$url);
   $rootpath="http://".$roopath[2]."/";
   $nnn=count($roopath)-1;for($yu=3;$yu<$nnn;$yu ){$rootpath.=$roopath[$yu]."/";}
   // var_dump($rootpath); //http: ,'',127.0.0.1,xnml,index.php
   }
   else{$rootpath=$url;//var_dump($rootpath);
  }
  if(isset($url)){
  echo "$url 有下列裢接:<br>";
  $fcontents = file($url);
  while(list(,$line)=each($fcontents)){
  while(eregi('(href[[:space:]]*=[[:space:]]*"?[[:alnum:]:@/._-] [?]?[^"]*"?)',$line,$regs)){
  //$regs[1] = eregi_replace('(href[[:space:]]*=[[:space:]]*"?)([[:alnum:]:@/._-] )("?)',"\2",$regs[1]);
  $regs[1] = eregi_replace('(href[[:space:]]*=[[:space:]]*["]?)([[:alnum:]:@/._-] [?]?[^"]*)(.*)[^"/]*(["]?)',"\2",$regs[1]);
  
  if(!eregi('^http://',$regs[1])){
  
   if(eregi('^..',$regs[1])){
   // $roopath=eregi_replace('(http://)?([[:alnum:]:@/._-] )[[:alnum:] ](.*)[[:alnum:] ]',"http://\2",$url);
  
   $roopath=split("/",$rootpath);
   $rootpath="http://".$roopath[2]."/";
   //echo "这是根本d :"."n";
   $nnn=count($roopath)-1;for($yu=3;$yu<$nnn;$yu ){$rootpath.=$roopath[$yu]."/";}
   //var_dump($rootpath);
   if(eregi('^..[/[:alnum:]]',$regs[1])){
   //echo "这是../目录/ :"."n";
   //$regs[1]="../xx/xxxxxx.xx";
   // $rr=split("/",$regs[1]);
   //for($oooi=1;$oooi<count($rr);$oooi )
  $rrr=$regs[1];
   // {$rrr.="/".$rr[$oooi];
   $rrr = eregi_replace("^[.][.][/]",'',$rrr); /

本文章给家收集了大量的关于html标签的去除方法,很多朋友可能会想到使用strip_tags函数,但这个函数会把所有的html标签全部删除了,下面我来给大家介绍去掉指定的html标签及内容方法,有需要了解的朋友可参考。

string strip_tags ( string str [, string allowable_tags] )

弊端 :

这个函数只能保留想要的html标签,就是参数string allowable_tags。
在yizero的评论中我知道了这个函数的参数allowable_tags的其他的用法。

 代码如下 复制代码

strip_tags($source, ”); 去掉所以的html标签。

strip_tags($source, ‘<div><img><em>’); 保留字符串中的div、img、em标签。

如果想去掉的html的指定标签。那么这个函数就不能满足需求了。于是乎我用到了这个函数。

 

 代码如下 复制代码
<?php
/**
* 删除指定的HTML标签及其中内容,暂时只支持单标签清理
*
* @param string $string -- 要处理的字符串
* @param string $tagname -- 要删除的标签名称
* @param boolean $clear -- 是否删除标签内容
* @return string -- 返回处理完的字符串
*/
function replace_html_tag($string, $tagname, $clear = false){
$re = $clear ? '' : '1';
$sc = '/<' . $tagname . '(?:s[^>]*)?>([sS]*?)?</' . $tagname . '>/i';
return preg_replace($sc, $re, $string);
}


以下是测试代码

 代码如下 复制代码

// 百度首页内容

$string = file_get_contents('http://www.111cn.net/');

// 去掉 style 及包含内容
$string = replace_html_tag($string, 'style', true);
$string = replace_html_tag($string, 'script', true);

// 去掉 a 标签,并保存其中内容
$string = replace_html_tag($string, 'a');

// 去掉 span 标签,并保存其中内容
$string = replace_html_tag($string, 'span');

echo $string;
?>

如果我们要删除指定两者之间的数据

 

 代码如下 复制代码
<?php
/**  
* PHP去掉特定的html标签
* @param array $string  
* @param bool $str 
* @return string
*/ 
function _strip_tags($tagsArr,$str) {  
    foreach ($tagsArr as $tag) { 
        $p[]="/(<(?:/".$tag."|".$tag.")[^>]*>)/i"; 
    } 
    $return_str = preg_replace($p,"",$str); 
    return $return_str; 

 
$str = "<b>您好</b><input type='text' name='' /><a href='http://www.baidu.com'>百度一下,你就知道</a>"; 
echo _strip_tags(array("b", "input", "a"),$str); #去掉 B 标签和 INPUT 标签 
?>
使用php的同学都会知道上传文件我们默认情况在php只会支持2MB文件上传大小了,如果超过了就无法上传了,下面我来给大家介绍如何设置PHP上传文件大小限制吧。

一,如果php没有限制我们可以在上传时就限制上传大小,如

 

 代码如下 复制代码
$maxattachsize=5097152;//最大上传大小,默认是2m
$temppath=$upfile['tmp_name'];
$filesize=filesize($temppath);
if($filesize > $maxattachsize)$err='文件大小超过'.$maxattachsize.'字节';

这样就限制了文件只能上传2MB大小的文件了


二,修改php.ini中的post_max_size和upload_max_filesize

1、memory_limit内存设置限制

2、max_execution_time程序执行时间限制

3、post_max_size最大POST数据限制

4、upload_max_filesize最大上传文件大小限制

 代码如下 复制代码

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60    ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M      ; Maximum amount of memory a script may consume (128MB)

; Maximum size of POST data that PHP will accept.
post_max_size = 105M

; Maximum allowed size for uploaded files.
upload_max_filesize = 100M

小提示,如果你是上传大文件我们最好在页面加上执行时间set_time_limit

 代码如下 复制代码

set_time_limit(0);

这样就不会超时了

有很多朋友都不知道怎么把数组中元素给删除,下面我来总结各种数组删除元素方法给各位,有需要了解的朋友可进入参考。

删除数组指定元素

 代码如下 复制代码

<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>

输出

Array ( [0] => Cat [1] => Horse )


几个函数用下来觉得array_search()比较实用

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false

 代码如下 复制代码


$array = array('1', '2', '3', '4', '5');

$del_value = 3;
unset($array[array_search($del_value , $array)]);//利用unset删除这个元素

print_r($array);

输出

array('1', '2', '4', '5');


从数组头删除值

array_shift()函数删除并返回数组中找到的元素。其结果是,如果使用的是数值健,则所有相应的值都会下移,而使用关联键的数组不受影响。其形式为:


mixed array_shift(array array)

下面的例子删除了$fruits数组中的第一个元素apple:

 代码如下 复制代码

$fruits = array("apple","banana","orange","pear");
$fruit = array_shift($fruits);
// $fruits = array("banana","orange","pear")
// $fruit = "apple";

从数组尾删除元素
array_pop()函数删除并返回数组的最后一个元素。其形式为:


mixed array_pop(aray target_array);

下面的例子从$states数组删除了最后的一个州:

 代码如下 复制代码

$fruits = array("apple","banana","orange","pear");
$fruit = array_pop($fruits);
//$fruits = array("apple","banana","orange");
//$fruit = "pear";


删除数组中间的元素

 代码如下 复制代码

function deleteElementFromArr($arr, $index){
if($index < count($arr)-1){
unset($arr[$index]);
reset($arr);
}
return $arr;
}

我封装成了一个函数,方便大家使用:

 代码如下 复制代码


<?php

function array_remove(&$arr, $offset)
{
array_splice($arr, $offset, 1);
}

$arr = array('apple','banana','cat','dog');

array_remove($arr, 2);
print_r($arr);
?>

删除数组重复元素

 代码如下 复制代码


<?php
function assoc_unique($arr, $key) {
$tmp_arr = array();
foreach($arr as $k => $v) {
if(in_array($v[$key], $tmp_arr)) {
unset($arr[$k]);
} else {
$tmp_arr[] = $v[$key];
}
}
sort($arr);
return $arr;
}

$aa = array(
array('id' => 123, 'name' => '张三'),
array('id' => 123, 'name' => '李四'),
array('id' => 124, 'name' => '王五'),
array('id' => 125, 'name' => '赵六'),
array('id' => 126, 'name' => '赵六')
);
$key = 'name';
assoc_unique(&$aa, $key);
print_r($aa);
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: