【问题标题】:search query doesnt work in firefox but does in other browsers?搜索查询在 Firefox 中不起作用,但在其他浏览器中起作用?
【发布时间】:2013-02-04 03:10:45
【问题描述】:

我有一个搜索查询脚本,它在 Firefox、Safari 等中运行良好,但在 chrome 中却不行。

无论出于何种原因,一旦用户输入查询并点击提交,我就会注意到网址非常不同。

在 chrome 中,我得到了这个有效且正确的方法

http://localhost/ptb1/home.php?query=london&submit.x=0&submit.y=0&submit=Start+Search

但在 Firefox 中我得到了这个(注意 stat+search 丢失了,我不知道为什么

http://localhost/ptb1/home.php?query=london&submit.x=12&submit.y=10

请谁能告诉我为什么我会遇到这个问题以及如何解决这个问题,谢谢。

这是我的代码:

<form method="get" action="">
<input type="text" id="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
</form>

<?php
//PHP CODE STARTS HERE

if(isset($_GET['submit'])){

// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="";
$db_tb_atr_name="display_name";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);


$query_for_result=mysql_query("SELECT *,MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST ('$query' IN BOOLEAN MODE) AS relevance FROM ptb_stats WHERE MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST('$query' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))

{

    echo "<div class=\"text\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
    echo "<div class=\"spacing\"><img width=35px height= 30px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> "; 
     echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div></div>";

}
echo "<div class=\"morebutton-search\"><a href=\"search_results.php?query=$query\" \">+ view more results</a></div>";




mysql_close();
}

?>



<style type="text/css">



.boxgridsearch{ 
                width: 35px; 
                height: 30px; 
                margin-top:-3px;
                margin-left:0px; 
                margin-right:10px; 
                float:left; 
                background:#161613; 
                        border:4px solid #000 .8;
    -moz-box-shadow: 0px 0px 8px #CCC;
    -webkit-box-shadow: 0px 0px 0px #CCC;
    box-shadow: 0px 3px 10px #030303;



            }


    .suggestionsBox {
        position: relative;
        left: 12px;
        margin: 10px 0px 5px 0px;
        width: 172px;
        background-color: #FFF;
        border: 1px solid #CCC; 
        color: #fff;
        z-index:99;
    }

    .searchbutton {

        padding-top: 2px;
        padding-left:5px;
        position:absolute;
        z-index:70;

    }


    .spacing {

        padding-top: 12px;
        padding-bottom:12px;
        border-bottom: solid #CCC 1px;
        border-top: solid #CCC 1px;
        text-align:left;
        margin-left:0px;
        margin-top:5px;
        width:160px;


    }

    .text {
        text-align:left;
        margin-left:20px;
        margin-top:0px;
        width:200px;


    }


        .search-results {
    width: 200px;
    height:218px;
    float: left;

    margin-bottom: 20px;
    margin-left: 0px;
    margin-top:10px;
    text-align: center;

    /* [disabled]margin-left: 30px; */
    overflow: hidden;
    padding-top: 0px;
    padding-bottom: 10px;


}   


    .searchbox {

        margin-left: -25px;





    }


    .suggestionList {
        margin: 0px;
        padding: 0px;

    }

    .suggestionList li {

        margin: 0px 0px 3px 0px;
        padding: 3px;
        cursor: pointer;
    }

    .suggestionList li:hover {
        background-color: #659CD8;
    }


    a.search {
    color: #646464;
    text-decoration: none;
}
a.search:visited {
    color: #646464;
}
a.search:hover {
    color: #646464;
    text-decoration: none;
}
a.search:active {
    color: #646464;
}




</style>

【问题讨论】:

    标签: php mysql firefox


    【解决方案1】:

    您可以尝试使用$_SERVER['REQUEST_METHOD'] 来确定页面是否已提交,而不是isset($_GET['submit']) 所以基本上改为:

    if(isset($_GET['submit'])){
    
    // Change the fields below as per the requirements
    $db_host="localhost";
    $db_username="root";
    

    你会的:

    if($_SERVER['REQUEST_METHOD'] == 'GET'){
    
    // Change the fields below as per the requirements
    $db_host="localhost";
    $db_username="root";
    

    【讨论】:

    • 是的,确实有效,谢谢,但只有一个问题,一旦用户点击提交,搜索结果应该显示在搜索框下方的下拉列表中,但现在下拉框一直在德雷尔。有什么办法可以解决这个问题吗?我已经用附加的 css 更新了我的代码。
    • @EricJohnson 因此无论是否提交了搜索,您都会看到下拉菜单,并且您希望它仅在用户提交搜索时显示。对吗?
    • @EricJohnson 您可以使用相同的 if 语句 (if($_SERVER['REQUEST_METHOD'] == 'GET')) 来检查表单是否已提交。如果是,您可以显示下拉菜单。这对你有用吗?
    【解决方案2】:

    浏览器会以不同的方式解释该表单的提交方式。您在 Firefox 中看到的行为实际上是起草的标准: http://www.whatwg.org/specs/web-apps/current-work/multipage/

    我们可以深入研究样式按钮与使用图像(提交、vs 图像输入类型、vs 按钮)——最简单的解决方法是在表单中添加一个隐藏的输入值:

    <form method="get" action="">
        <input type="hidden" name="dosearch" value="1">
        <input type="text" id="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
        <input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
    </form>
    

    然后在你的 PHP 中调整处理条件:

    if(isset($_GET['dosearch'])){
    

    快速补丁。

    如果你打算做很多这样的事情,你应该考虑一个框架;您可以在非常干净和分层的代码 (MVC) 中免费获得大量此类逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2023-04-06
      • 2016-04-20
      • 2020-03-29
      • 2011-07-22
      • 2013-09-29
      • 1970-01-01
      相关资源
      最近更新 更多