PHP:选中键盘上的按下按钮

php: check pressed button on keyboard

本文关键字:按钮 键盘 PHP      更新时间:2023-09-26

我的页面中有以下结构: page.php

<html>
    <head>
        <title>Page title</title>
        <script src="jquery-2.0.3.js"></script>
        <script type="text/javascript">
            function search(value){
                window.location.href="page.php?search=" + value;
            }
        </script>
    </head>
    <body>
        <?php
            if(!empty($_GET['search'])){
                //SQL-statement with WHERE column LIKE $_GET['search']
            } else {
                //Other SQL-statement without WHERE column LIKE $_GET['search']
            }
        ?>
        <h1>Title</h1>
        <input type="text" onKeyUp="search(this.value);" 
               value="<?php echo $_GET['search']; ?>">
    </body>
</html>

正如你所读到的,这很重要,当按下spacebar时,它会在$_GET['search']和我的input字段中放置一个_,这是一个&nsbp;

value' '(单个空格)和 window.location.href="page.php?search=" + value;将被 URL 编码,因此空格将转换为&nbsp;(URL 实体)

你必须urldecode($_GET['search']);才能把它变回一个太空角色。(http://us2.php.net/urldecode)

也许尝试这样的替换:

var new_string = old_str.replace("&nbsp;", "_");

试试这个,

<?php
    if(isset($_GET['search']) && !empty($_GET['search'])){
         $q=urldecode($_GET['search']);// use it in your query
         //SQL-statement with WHERE column LIKE $_GET['search']
    } else {
        //Other SQL-statement without WHERE column LIKE $_GET['search']
    }
?>

读取网址解码()