使用mootools

时间:2015-07-26 12:25:24

标签: javascript php html mysql mootools

我试图用表单搜索my​​sql数据库(用phpMyAdmin制作),结果更新图像。该数据库由2列组成,ID(自动递增主键)和Image(url / path to image)。搜索仅检查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";
}

编辑:on onccess将图像alt文本更改为响应,表明第2行的名称上存在未定义的索引错误。

EDIT2:进步!以下php代码将相应的图像url设置为alt文本,但图像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路径......现在工作正常。

2 个答案:

答案 0 :(得分:1)

您不在SQL语句中选择“Image”列。

您的代码应该是: $sql="SELECT Image, ID FROM image WHERE ID LIKE '%" . $name ."%'"; 代替 $sql="SELECT ID FROM image WHERE ID LIKE '%" . $name ."%'";

答案 1 :(得分:0)

您在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'];