简体   繁体   中英

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

  • There is a typo in <img src="images/profilepic/'.$r["profile_pic"].'alt="Avatar" class="circlemsg"> It need to be <img src="images/profilepic/'.$r["profile_pic"].'" alt="Avatar" class="circlemsg">

  • Also, profile_pic does have also the extension (ie: 'jpg, png') ?

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;

}

Ok..After working many times on it I have found a solution of it, and which is working for me. It is like that

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 .=
'<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>';

   }


$output .= '</table>';
echo $output;

}

After this, my entire row is clickable now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM