($width) AND $width .= 'px';
$style = " style=\"width: $width\"";
}
$value = $value ? $value : date('H:i');
$s = "";
return $s;
}
// form_date('start', '2018-07-05') 为空则当前日期
function form_date($name, $value = 0, $width = FALSE)
{
$style = '';
if (FALSE !== $width) {
is_numeric($width) AND $width .= 'px';
$style = " style=\"width: $width\"";
}
$value = $value ? $value : date('Y-m-d');
$s = "";
return $s;
}
/**用法
*
* echo form_radio_yes_no('radio1', 0);
* echo form_checkbox('aaa', array('无', '有'), 0);
*
* echo form_radio_yes_no('aaa', 0);
* echo form_radio('aaa', array('无', '有'), 0);
* echo form_radio('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'b');
*
* echo form_select('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'a');
*/
?>组保留的标签 余下为需要删除的标签
unset($oldtag[$key]);
}
}
}
if (!empty($oldtag)) {
$tagids = array();
foreach ($oldtag as $tagid => $tagname) {
$tagids[] = $tagid;
}
well_oldtag_delete($tagids, $tid);
}
$r = well_tag_process($tid, $fid, $create_tag, $tagarr);
return $r;
}
// 删除标签和绑定的主题
function well_oldtag_delete($tagids, $tid)
{
$pagesize = count($tagids);
$arrlist = well_tag_find_by_tagids($tagids, 1, $pagesize);
$delete_tagids = array(); // 删除
$tagids = array();
$n = 0;
foreach ($arrlist as $val) {
++$n;
if (1 == $val['count']) {
// 只有一个主题
$delete_tagids[] = $val['tagid'];
} else {
$tagids[] = $val['tagid'];
}
}
!empty($delete_tagids) and well_tag_delete($delete_tagids);
$arlist = well_tag_thread_find_by_tid($tid, 1, $n);
if ($arlist) {
$ids = array();
foreach ($arlist as $val) $ids[] = $val['id'];
well_tag_thread_delete($ids);
}
!empty($tagids) and well_tag_update($tagids, array('count-' => 1));
}
// 标签数据处理 $arr=新提交的数组 $tagarr=保留的旧标签
function well_tag_process($tid, $fid, $new_tags = array(), $tagarr = array())
{
if (empty($tid)) return '';
// 新标签处理入库
if ($new_tags) {
$threadarr = array();
$tagids = array();
$i = 0;
$size = 5;
$n = count($tagarr);
$n = $n > $size ? $size : $size - $n;
foreach ($new_tags as $name) {
++$i;
$name = trim($name);
$name = stripslashes($name);
$name = strip_tags($name);
$name = str_replace(array(' ', '#', "@", "$", "%", "^", '&', '·', '<', '>', ';', '`', '~', '!', '¥', '……', ';', '?', '?', '-', '—', '_', '=', '+', '.', '{', '}', '|', ':', ':', '、', '/', '。', '[', ']', '【', '】', '‘', ' ', ' ', ' ', ' ', ' '), '', $name);
$name = htmlspecialchars($name, ENT_QUOTES);
if ($name && $i <= $n) {
// 查询标签
$read = well_tag_read_name($name);
if ($read) {
// 存在 count+1
$tagids[] = $read['tagid'];
} else {
// 入库
$arr = array('name' => $name, 'count' => 1);
$tagid = well_tag_create($arr);
FALSE === $tagid and message(-1, lang('create_failed'));
$read = array('tagid' => $tagid, 'name' => $name);
}
$tag_thread = array('tagid' => $read['tagid'], 'tid' => $tid);
$threadarr[] = $tag_thread;
$tagarr[$read['tagid']] = $read['name'];
}
}
!empty($threadarr) and tag_thread_big_insert($threadarr);
!empty($tagids) and well_tag_update($tagids, array('count+' => 1));
}
$json = empty($tagarr) ? '' : xn_json_encode($tagarr);
return $json;
}
?>return $r;
}
/**
* @param int $page 页数
* @param int $pagesize 每页显示数量
* @return mixed
*/
function link_find($page = 1, $pagesize = 100)
{
$arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize);
return $arr;
}
/**
* @param $id
* @return bool 返回FALSE失败 TRUE成功
*/
function link_delete($id)
{
if (empty($id)) return FALSE;
$r = link__delete(array('id' => $id));
link_delete_cache();
return $r;
}
//--------------------------kv + cache--------------------------
/**
* @return mixed 返回全部友情链接
*/
function link_get($page = 1, $pagesize = 100)
{
$g_link = website_get('friends_link');
if (empty($g_link)) {
$g_link = link_find($page, $pagesize);
$g_link AND website_set('friends_link', $g_link);
}
return $g_link;
}
// delete kv and cache
function link_delete_cache()
{
website_set('friends_link', '');
return TRUE;
}
?> $v = implode(",", $v);
$temp[] = $v;
}
// 去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
// 再将拆开的数组重新组装
$output = array();
foreach ($temp as $k => $v) {
if ($stkeep) $k = $starr[$k];
if ($ndformat) {
$temparr = explode(",", $v);
foreach ($temparr as $ndkey => $ndval) $output[$k][$ndarr[$ndkey]] = $ndval;
} else $output[$k] = explode(",", $v);
}
return $output;
}
// 合并二维数组 如重复 值以第一个数组值为准
function array2_merge($array1, $array2, $key = '')
{
if (empty($array1) || empty($array2)) return NULL;
$arr = array();
foreach ($array1 as $k => $v) {
isset($v[$key]) ? $arr[$v[$key]] = array_merge($v, $array2[$k]) : $arr[] = array_merge($v, $array2[$k]);
}
return $arr;
}
/*
* 对二维数组排序 两个数组必须有一个相同的键值
* $array1 需要排序数组
* $array2 按照该数组key排序
* */
function array2_sort_key($array1, $array2, $key = '')
{
if (empty($array1) || empty($array2)) return NULL;
$arr = array();
foreach ($array2 as $k => $v) {
if (isset($v[$key]) && $v[$key] == $array1[$v[$key]][$key]) {
$arr[$v[$key]] = $array1[$v[$key]];
} else {
$arr[] = $v;
}
}
return $arr;
}
?>
Ajax-软件玩家 - 软件改变生活!
Ajax
跨域请求:两个不同域名之间的通讯。
Ajax受到浏览器的限制(安全性考虑)不允许跨域通信。
<script>
var req=new XMLHttpRequest();
req.open('get','.js'); //报错
req.send();
</script>
默认情况下浏览器采用同源策略:一个域下面的JS只能请求同一个域下面的文档内容,不能跨域请求。
解决方法一:src属性
使用标签的src属性,src属性是没有同源的限制的
<img src='.jpg' />
<script src='.js'></script>
解决方法二:PHP代理
A客户端
<script>
var req=new XMLHttpRequest();
req.open('post','./6-demo.php');
req.onreadystatechange=function(){if(req.readyState==4 && req.status==200){alert(req.responseText);}
}
req.send();
</script>
A服务器
<?php
echo file_get_contents('.php');
B服务器
<?php
echo '锄禾日当午';
小结:客户端有同源策略,服务器没有同源策略,我们可以使用服务器作为代理去跨域请求。
A服务器()
B服务器()
解决方法三:jsonp
jsonp=json+动态script。是一种非官方协议,为了解决JS的跨域请求
通过 <script> 的src属性引入跨域文件从而携带数据实现了数据的跨域访问。
代码实现
客户端代码(www.php.com)
<body>
<script>
function fun(data){alert(data);
}
window.onload=function(){document.getElementById('btn').onclick=function(){var script=document.createElement('script');script.src='.php?fn=fun'; //动态添加script的src属性document.body.appendChild(script);}
}
</script>
<input type="button" value="确定" id='btn'>
</body>
PHP代码()
<?php
$stu=array('tom','berry');
$data=json_encode($stu);
echo "{$_GET['fn']}($data)"; //拼接函数的调用
注意:
1、通过动态的添加script的src属性
2、传递的函数名和客户端定义的函数名要一致
3、跨域的服务器返回的是调用函数的形式
解决方法四:CORS实现跨域
设置允许接受的源
header('Access-Control-Allow-Origin:'); //允许php.com访问
CORS(Cross-Origin Resource Sharing)跨源资源共享:服务器在响应头中告知浏览器那些域请求限制,哪些域请求接受。
客户端代码()
<body>
<script>
window.onload=function(){document.getElementById('btn').onclick=function(){var req=new XMLHttpRequest();req.open('post','.php');req.onreadystatechange=function(){if(req.readyState==4)alert(req.responseText);}req.send()}
}
</script>
<input type="button" value="确定" id='btn'>
</body>
服务器端代码()
header('Access-Control-Allow-Origin:*'); //允许所有的域名访问
echo 'i am a boy';
本文标签:
Ajax
更多相关文章
Ajax请求URL地址,并在ajax请求中打开这个地址,被浏览器禁止问题解决办法
最近遇到一个需求,前端传给后端用户信息,经过后端加密后,返回前端一个URL地址,要求前端拿到请求回来的URL地址,并跳转到URL所
jquery中的ajax封装逻辑,jQuery里面ajax请求的封装
为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码: function api_request(name, params, cb, scope, asyn
javascript - Difference between fetch, ajax, and xhr - Stack Overflow
What is the difference between these 3 calling methods? I'm using fetch in my current project and
解决浏览器直接get能获取数据而ajax post却不能
分析 打开控制台发现会有这条错误信息: has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header co… 而直接get是有数据的,说明服务器是正
javascript - Google Storage API - Resumable Upload, AJAX, CORS Error - Stack Overflow
Getting a strange error.When a user wants to upload a file, they send an AJAX request to my server. My
javascript - Nonces, AJAX, script variables & security in WordPress
Alright, so let's say you develop a website where you enqueue a js script on a page X of your frontend, using:wp_e
javascript - jQuery, ajax, display page inside of other page (like iframe) - Stack Overflow
I need to display a page inside of a page, like an iframe, but using jquery. The pages are not on the s
Get id of element when click ( php, jquery, ajax, javascript ) - Stack Overflow
I'm sorry, this is my first project and I'm learning a lot. So if someone can help me, I will
javascript - Datatables refresh with draw(), ajax, pagination - Stack Overflow
I'm trying to refresh my table with data from the server every few seconds.It's loading the
发表评论