【问题标题】:Live search not working with apostrophies php/javascript实时搜索不适用于撇号 php/javascript
【发布时间】:2011-03-17 15:58:19
【问题描述】:

我有这个实时搜索,在我的 mysql 数据库中的公司名称中有撇号之前,它可以完美运行。一个例子——上厕所了。 我使用的搜索来自使用 php5 开发它的人。我已经设法修改代码以使用我的代码,但是在从列表中选择一个带有撇号的名称时失败了。当我选择名称时,输入框留空。

<div>
    <?  print"<form method=\"post\" action=ordernew.php?action=startneworder>"; ?>
        <div>
            Customer Name:
            <br />
            <input type="text" size="30" name="company" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
        </div>

        <div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
        </div>
        <? print"<input type=submit value=Continue />"; ?>
    </form>
</div>

上面的代码是我表单上的代码。下面的代码就是所有的工作。

<?php
require_once("functions.php");

//dbconn(假); //loggedinonly();
// PHP5 实现 - 使用 MySQLi。 // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); $db = new mysqli($mysql_host, $mysql_user ,$mysql_pass, $mysql_db);

if(!$db) {
    // Show error if we cannot connect.
    echo 'ERROR: Could not connect to the database.';
} else {
    // Is there a posted query string?
    if(isset($_POST['queryString'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);

        // Is the string length greater than 0?

        if(strlen($queryString) >0) {
            // Run the query: We use LIKE '$queryString%'
            // The percentage sign is a wild-card, in my example of countries it works like this...
            // $queryString = 'Uni';
            // Returned data = 'United States, United Kindom';

            // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
            // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10

            $query = $db->query("SELECT company FROM eventcompany WHERE company LIKE '$queryString%' LIMIT 10");
            if($query) {
                // While there are results loop through them - fetching an Object (i like PHP5 btw!).
                while ($result = $query ->fetch_object()) {
                    // Format the results, im using <li> for the list, you can change it.
                    // The onClick function fills the textbox with the result.

                    // YOU MUST CHANGE: $result->value to $result->your_colum
                    echo '<li onClick="fill(\''.$result->company.'\');">'.$result->company.'</li>';
                    //echo '<li onClick="fill(\"'.$result->company.'\");">'.$result->company.\"</li>';
                }
            } else {
                echo 'ERROR: There was a problem with the query.';
            }
        } else {
            // Dont do anything.
        } // There is a queryString.
    } else {
        echo 'There should be no direct access to this script!';
    }
}?>

这部分在我的标题中。

<script type="text/javascript">
function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("backend/rpc.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {
    $('#inputString').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}

我认为问题在于点击事件。它适用于 & 字符,但不适用于 ' 。 欢迎任何建议。 非常感谢

史蒂夫

我尝试显示图像,但代表点低。 感谢您的回复。我现在得到了这个 loo\'s 显示但是当我选择选项输入框仍然是空白的。这只发生在带有 ' 的公司中。我知道没有多少公司在其中,但现在它更多或一个挑战。

排序你快到了。

$company = addslashes($result->company);
   echo "<li onClick=\"fill('".$company."');\">".$result->company."</li>";

在萤火虫的帮助下。非常感谢,顺便说一下,我注意到第一列需要添加斜杠,而第二列不需要。所以我想你可能只是把它们弄错了;)谢谢你的帮助。

史蒂夫

【问题讨论】:

  • 错误信息是什么?它来自 PHP 还是 Javascript?
  • 没有错误信息。当我的值中有撇号时,我的表单输入框中的输入框为空。如果该值是一个普通的单词,它可以正常工作。

标签: php search live


【解决方案1】:

答案是额外的引号弄乱了你的 HTML。

使用贵公司的“loo's”示例,此行:

echo '<li onClick="fill(\''.$result->company.'\');">'.$result->company.'</li>';

将打印 HTML:

<li onClick="fill('loo's');">loo's</li>';

问题在于这里额外的单引号:

onClick="fill('loo's');"

需要打印的是:

onClick="fill('loo\'s');"

修复它的最简单方法是将PHP更改为:

$company = addslashes($result->company);
echo "<li onClick=\"fill('".$result->company."');\">".$company."</li>";

我没有测试代码来验证,但这应该可以解决问题。

另外,请尝试在 Firefox 中运行该页面。安装 Firebug 插件。您可以在 Firebug 的控制台选项卡中查看通过 Ajax 返回的内容。这将对您将来有所帮助。

【讨论】:

  • 我尝试显示图像,但我的代表点较低。感谢您的回复。我现在得到了这个 loo\'s 显示但是当我选择选项输入框仍然是空白的。这只发生在带有 ' 的公司中。我知道没有多少公司在其中,但现在它更多或一个挑战。
  • 我安装了 firefox firebug,它给了我这个错误缺失)在参数列表website.co.uk/dir/new.php Line 1 fill('loo's Ltd');
猜你喜欢
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多