Php 使用Jquery自动完成图像
我正在使用foxycomplete的代码bellow从我的数据库中获取一些带有图像的结果。 使用本地文件非常好,但我不能使用mysql 这是我正在使用的代码Php 使用Jquery自动完成图像,php,jquery,Php,Jquery,我正在使用foxycomplete的代码bellow从我的数据库中获取一些带有图像的结果。 使用本地文件非常好,但我不能使用mysql 这是我正在使用的代码 <form id="autocompleteForm" name="autocompleteForm" action="" method="post"> <input type="text" name="s" id="s" value="Search" style="border:none;" /> <
<form id="autocompleteForm" name="autocompleteForm" action="" method="post">
<input type="text" name="s" id="s" value="Search" style="border:none;" />
</form>
Javascript代码:
(function($) {
$(document).ready(function() {
$( '#s' ).each( function(){
$(this).attr( 'title', $(this).val() )
.focus( function(){
if ( $(this).val() == $(this).attr('title') ) {
$(this).val( '' );
}
} ).blur( function(){
if ( $(this).val() == '' || $(this).val() == ' ' ) {
$(this).val( $(this).attr('title') );
}
} );
} );
$('input#s').result(function(event, data, formatted) {
$('#result').html( !data ? "No match!" : "Selected: " + formatted);
}).blur(function(){
});
$(function() {
function format(mail) {
return "<a href='"+mail.permalink+"'><img src='" + mail.image + "' /><span class='title'>" + mail.title +"</span></a>";
}
function link(mail) {
return mail.permalink
}
function title(mail) {
return mail.title
}
$("#s").autocomplete(completeResults, {
width: $("#s").outerWidth()-2,
max: 5,
scroll: false,
dataType: "json",
source: "video_search.php",
matchContains: "word",
parse: function(data) {
return $.map(data, function(row) {
return {
data: row,
value: row.title,
result: $("#s").val()
}
});
},
formatItem: function(item) {
return format(item);
}
}).result(function(e, item) {
$("#s").val(title(item));
//location.href = link(item);
});
});
});
})(jQuery);
<?php
require "connectiondb.php";
if(isset($_REQUEST['s']))
{
$queryString = mysql_real_escape_string($_REQUEST['s']);
echo'var completeResults = [{';
$ss = mysql_query ("SELECT title, image FROM users WHERE title LIKE '$queryString%' LIMIT 7");
while($result = mysql_fetch_assoc($ss)) {
echo '
"permalink": "index.html",
"image": "'.$result['image'].'",
"title": "'.$result['title'].'"
},';
}
echo '}];';
}else{
}
?>
(函数($){
$(文档).ready(函数(){
$('#s')。每个(函数(){
$(this.attr('title',$(this.val())
.focus(函数(){
if($(this.val()==$(this.attr('title')){
$(this.val(“”);
}
}).blur(函数(){
if($(this.val()=''| |$(this.val()=''){
$(this.val($(this.attr('title'));
}
} );
} );
$('input#s')。结果(函数(事件、数据、格式化){
$(“#结果”).html(!数据?“不匹配!”:“选中:”+格式化);
}).blur(函数(){
});
$(函数(){
函数格式(邮件){
返回“”;
}
功能链接(邮件){
return mail.permalink
}
功能名称(邮件){
返回邮件.title
}
$(“#s”).autocomplete(completeResults{
宽度:$(“#s”).outerWidth()-2,
最高:5,
卷轴:错,
数据类型:“json”,
来源:“video_search.php”,
“单词”,
解析:函数(数据){
返回$.map(数据、函数(行){
返回{
数据:世界其他地区,
值:row.title,
结果:$(“#s”).val()
}
});
},
formatItem:函数(项){
返回格式(项目);
}
}).结果(功能(e,项目){
$(#s)val(标题(项目));
//location.href=链接(项目);
});
});
});
})(jQuery);
和检索我的数据的php代码:
(function($) {
$(document).ready(function() {
$( '#s' ).each( function(){
$(this).attr( 'title', $(this).val() )
.focus( function(){
if ( $(this).val() == $(this).attr('title') ) {
$(this).val( '' );
}
} ).blur( function(){
if ( $(this).val() == '' || $(this).val() == ' ' ) {
$(this).val( $(this).attr('title') );
}
} );
} );
$('input#s').result(function(event, data, formatted) {
$('#result').html( !data ? "No match!" : "Selected: " + formatted);
}).blur(function(){
});
$(function() {
function format(mail) {
return "<a href='"+mail.permalink+"'><img src='" + mail.image + "' /><span class='title'>" + mail.title +"</span></a>";
}
function link(mail) {
return mail.permalink
}
function title(mail) {
return mail.title
}
$("#s").autocomplete(completeResults, {
width: $("#s").outerWidth()-2,
max: 5,
scroll: false,
dataType: "json",
source: "video_search.php",
matchContains: "word",
parse: function(data) {
return $.map(data, function(row) {
return {
data: row,
value: row.title,
result: $("#s").val()
}
});
},
formatItem: function(item) {
return format(item);
}
}).result(function(e, item) {
$("#s").val(title(item));
//location.href = link(item);
});
});
});
})(jQuery);
<?php
require "connectiondb.php";
if(isset($_REQUEST['s']))
{
$queryString = mysql_real_escape_string($_REQUEST['s']);
echo'var completeResults = [{';
$ss = mysql_query ("SELECT title, image FROM users WHERE title LIKE '$queryString%' LIMIT 7");
while($result = mysql_fetch_assoc($ss)) {
echo '
"permalink": "index.html",
"image": "'.$result['image'].'",
"title": "'.$result['title'].'"
},';
}
echo '}];';
}else{
}
?>
$result['image'中有什么内容?如果它包含图像文件本身(二进制数据),而不仅仅是路径,那么您需要再创建一个文件(image.php?s=id),该文件将仅检索具有足够头的二进制数据。。即
<?php
header('Content-Type: image/jpeg');
// sql stuff
echo $result['image'];
?>
另外,不要折磨自己,使用json_encode():)您是否需要在“源代码”中包含完整的URL,而不仅仅是php文件名?$('#s')。每个…
?应该只有一个$(“#s”)
-ID应该是唯一的打开您的控制台(在Firefox中使用firebug扩展,在Chrome中右键单击,选择Inspect元素,单击控制台选项卡),您可能会看到404错误。如果有错误,请告诉我们有哪些错误。@我得到了这个错误:加载资源失败:服务器以404(未找到)的状态响应@IreneT。猜测是这个文件video\u search.php
-您需要输入该文件的正确路径