2

I'm new to this and after searching for awhile, I thought I'd ask.

I've created a very basic search function which calls up the title names of the topics covered in a MySQL database through Ajax/jQuery.

I'm trying to now create a link so that if you want to learn more about the topic, then you can click and go to the detailed MySQL database record which has a lot more information.

Right now my search.php has the following:

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

The database has the following fields: title, subtitle01, subtext01, subtitle02, subtext02 and post_ID

I would like to add another string that would recognize the post_id that the title is based on and then create a link to view the details (which would display all the fields) on a new php page. Any thoughts on how to do this?

2

2 Answers 2

2

This?

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";
    }
}

edit: fixed typo

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks to the comments. The code above works. I'll also look into MVC. Thanks everyone.
0

If you really want to create search functionality with jquery-ajax and php, mysql.I have created a script for this at my website, here is the example code of javascript for search functionality.

    $(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;
}

If you want to download or learn full source code then you can follow the link.

Search With Jquery-Ajax, Php and Mysql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.