【发布时间】:2015-07-26 12:25:23
【问题描述】:
我正在尝试使用表单搜索 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 更改图像替代文本以响应显示第 2 行中的名称存在未定义的索引错误。
EDIT2:进步!以下 php 代码获取要设置为 alt 文本的适当图像 url,但图像 src 无法更改(因此显示 alt 文本)。如何让图像 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 路径中使用了反斜杠...现在可以正常工作了。
【问题讨论】:
-
我想你忘了给你发送的数据命名。试试
myRequest.send('name=' + searchText.value); -
这没有帮助。此外,我更改了 onRequest、onFailure 和 onSuccess 以更改图像替代文本,看起来请求成功。
-
哦,你用的是
method: 'get',,那么你在PHP上用var_dump($_GET);会得到什么? -
array(1){["name"]=>string(1)"2"} 用于输入“2”。
标签: javascript php html mysql mootools