问题描述
我正在尝试使用一种形式搜索mysql数据库(由phpMyAdmin制造),其结果将更新图像。 该数据库由2列组成,ID(自动递增主键)和Image(图像的URL /路径)。 搜索仅检查ID。
我非常确定问题是我没有访问myRequest.send的输入参数,也没有从php代码返回onSuccess期望的响应值。
的test.html
<!doctype html>
<html>
<head>
<title>UI Test</title>
<script type="text/javascript" src="MooTools-Core-1.5.1.js"></script>
<link rel="stylesheet" type="text/css" href="res/UI.css"/>
</head>
<body>
<script>
var searchForm = new Element('form', {
id: 'search_form',
method: 'post'
});
var searchText = new Element('input', {
id: 'search_text',
type: 'text',
name: 'name'
});
var searchButton = new Element('input', {
id: 'search_button',
type: 'submit',
name: 'submit',
value: 'Search'
});
var image = new Element('img', {
id: 'image_',
src: 'res/img/default.png',
alt: 'Image',
height: '50',
width: '50',
position: 'relative',
float: 'right'
});
var myRequest = new Request({
url: 'test.php',
method: 'get',
onRequest: function(){
image.set('alt','loading...');
},
onSuccess: function(response){
image.set('alt', response);
image.set('src', response);
},
onFailure: function(){
image.set('src', 'res/img/fail.png');
image.set('alt','failing...');
}
});
searchText.inject(searchForm);
searchButton.inject(searchForm);
searchForm.inject(document.body);
image.inject(document.body);
window.addEvent('domready', function(){
searchButton.addEvent('click', function(event){
event.stop();
myRequest.send('name='+searchText.value);
});
});
</script>
</body>
</html>
test.php的
if(preg_match("/^[ 0-9]+/", $_POST['name'])){
$name=$_POST['name'];
//-connect to the database
$db=mysql_connect ("localhost", "root", "password") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("tutorial");
//-query the database table
$sql="SELECT ID FROM `image` WHERE ID LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
if (false === $result) {
echo mysql_error();
}
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$ID=$row['ID'];
$image=$row['Image'];
//-want to return image path here
echo $image;
}
}
else{
echo "res\img\fail.png";
}
编辑:具有onSuccess更改图像alt文本以响应显示在第2行的名称上存在未定义的索引错误。
EDIT2:进展! 以下php代码获取了要设置为替代文本的适当图像url,但是图像src无法更改(因此显示了替代文本)。 我怎样才能使图像src正确更改?
<?php
$name=$_GET['name'];
if(preg_match("/^[ 0-9]+/", $name)){
//-connect to the database
$db=mysql_connect ("localhost", "root", "password") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("tutorial");
//-query the database table
$sql="SELECT Image, ID FROM image WHERE ID LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
if (false === $result) {
echo mysql_error();
}
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$ID=$row['ID'];
$image=$row['Image'];
//-want to return image path here
echo $image;
}
}
else{
echo "lulz";
}
?>
EDIT3:不好,我在图像src路径中使用反斜杠...现在可以正常工作了。
1楼
Luka Govedi?
1
2015-07-26 13:39:16
您无需在SQL语句中选择“图像”列。
您的代码应为: $sql="SELECT Image, ID FROM image WHERE ID LIKE '%" . $name ."%'";
$sql="SELECT Image, ID FROM image WHERE ID LIKE '%" . $name ."%'";
而不是$sql="SELECT ID FROM image WHERE ID LIKE '%" . $name ."%'";
$sql="SELECT ID FROM image WHERE ID LIKE '%" . $name ."%'";
2楼
Sergio
0
2015-07-27 10:06:02
您正在MooTools Request类中使用method: 'get',
则必须在PHP中使用$_GET
。
更换
if(preg_match("/^[ 0-9]+/", $_POST['name'])){
$name = $_POST['name'];
至
if(preg_match("/^[ 0-9]+/", $_GET['name'])){
$name = $_GET['name'];