javascriptphpajaxmysqlilivesearch

How to make an entire row clickable in live search


i am working on a live search.After writing a link in a row using ajax it stop responding

it is a code i have made

//let this page name is index.php

<div>
      <input type="search" id="searchterm" name="searchterm" placeholder="Search" >   
</div>
<table id="result"></table>

//this is the script of index.page

<script>
$(document).ready(function(){
    load_data();
    function load_data(query)
    {
        $.ajax({
            url:"search.php",
            method:"post",
            data:{query:query},
            success:function(data)
            {
                $('#result').html(data);
            }
        });
    }



$('#searchterm').keyup(function(){
        var search = $(this).val();
        if(search != '')
        {
            load_data(search);
        }
        else
        {
            load_data();            
        }
    });
});
</script>

//this page is search.php

$output='';
if(isset($_POST["query"]))
 {
    $keyword=mysqli_real_escape_string($conn, $_POST["query"]);
    $query="SELECT uid,name,lastname ,profile_pic,about FROM comnet_user_details WHERE uid!='$uid' AND concat_ws(' ',name, lastname) LIKE UPPER('%$keyword%')";

 $result=mysqli_query($conn,$query);



if(mysqli_num_rows($result) > 0)
 {
while($r=mysqli_fetch_array($result))
    {
        $nm=$r["name"]." ".$r["lastname"];
        $profilepic=$r["profile_pic"];
        $about=$r["about"];
        $id=$r["uid"];





  $output .=
     '<table class="msgtab" id="fetchresult">

      <tr  class="trow" onclick="location.href="conversation.php?value='.$id.'"">

     <td class="msg-col-1"><img src="images/profilepic/'.$r["profile_pic"].'alt="Avatar" class="circlemsg"></td>

     <td class="msg-col-2">
        <h5 class="msgheading">'.$nm.'</h5>
        <p class="msgcontent">'.$about.'</p>

        </td>
        </tr>;
        }
         echo $output;
}

I expect live search result row should be clickable and clicking on a particular result or row it should lead to the desire page, but my live search results link is not working


Solution

  • Edit:

    Could you replace the piece of code with this one (hope it will work): - create a table - in the while loop -> create every row <tr> - then close the table

    if(mysqli_num_rows($result) > 0) {
    
       $output = '<table class="msgtab" id="fetchresult">'; 
    
    while($r=mysqli_fetch_array($result))
        {
            $nm=$r["name"]." ".$r["lastname"];
            $profilepic=$r["profile_pic"];
            $about=$r["about"];
            $id=$r["uid"];
    
    
        $output .=
        '<a href="conversation.php?value='.$id.'">
        <tr class="trow">
        <td class="msg-col-1"><img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg"></td>
        <td class="msg-col-2">
        <h5 class="msgheading">'.$nm.'</h5>
        <p class="msgcontent">'.$about.'</p>
        </td>
        </tr>
        </a>';
    
        }
    
    $output .= '</table>';
    
    echo $output;
    
    }