使用MySQL,PHP,jQuery和Ajax创建搜索功能很困难

时间:2011-02-15 00:07:55

标签: php jquery mysql ajax

我是新手,经过一段时间的搜索,我想我会问。

我创建了一个非常基本的搜索功能,通过Ajax / jQuery调用MySQL数据库中涵盖的主题的标题名称。

我正在尝试创建一个链接,这样如果您想了解有关该主题的更多信息,那么您可以单击并转到详细的MySQL数据库记录,其中包含更多信息。

现在我的search.php有以下内容:

if (mysql_num_rows($result) > 0){
      while($row = mysql_fetch_object($result)){
        $string .= "<b>".$row->title."</b>";
        $string .= "<br/>\n";

该数据库包含以下字段:title,subtitle01,subtext01,subtitle02,subtext02和post_ID

我想添加另一个字符串来识别标题所基于的post_id,然后在新的php页面上创建一个链接来查看详细信息(显示所有字段)。有关如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:2)

此?

if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_object($result)){
        $string .= "<a href='detail.php?id=".$row->post_ID."'>".$row->title."</a><br />\n";
    }
}

编辑:修复拼写错误

答案 1 :(得分:0)

如果你真的想用jquery-ajax和php创建搜索功能,mysql.I已经在我的网站上为此创建了一个脚本,这里是javascript搜索功能的示例代码。

    $(document).ready(function(){

    // initilize varibales
    var htmlData='';

    var ajaxUrl='search.php';
    $("#searchText").keyup(function(){
        $searchText=$(this).val();
        if($searchText.length>=1){
            get_search_data($searchText,ajaxUrl,htmlData);
        }else{
            $(".search_result").html('').css("padding","0px").fadeOut();
        }
    });
});

function get_search_data($searchText,ajaxUrl,htmlData){
    $.ajax({
        url:ajaxUrl,
        type:"post",
        dataType:"json",
        data:{'action':'get_result','search_text':$searchText},
        success:function(response){
            console.log(response);
            for(var i=0; i<response.totalResult; i++){
                htmlData+='<p>'+response.allData[i].email+'</p>';
                $(".search_result").html(htmlData).css("padding","10px").fadeIn();
            }
        }
    });
    // return htmlData;
}

如果您想下载或学习完整源代码,可以点击链接。

Search With Jquery-Ajax, Php and Mysql