0

我正在尝试制作一个 webapp,并且我使用了另一个站点的 php、ajax 和 mysql 搜索功能。

它目前允许我搜索数据库并返回我想要的。我遇到的唯一问题是它在一个简单的文本框中返回结果。我希望能够在表格中返回结果。

搜索只是在数据库中搜索名字、姓氏、地址等......这是我拥有的代码。

php:

<?php

include('config.php');

if(isset($_GET['search_word']))
{
$search_word=$_GET['search_word'];

$sql=mysql_query("SELECT * FROM info WHERE CONCAT(Forename,' ',Surname) LIKE '%$search_word%'or Address1 LIKE '%$search_word%' or Address2 LIKE '%$search_word%' or Postcode LIKE '%$search_word%' or DOB LIKE '%$search_word%' ORDER BY ID DESC LIMIT 20 ");
$count=mysql_num_rows($sql);

if($count > 0)
{

while($row=mysql_fetch_array($sql))
{
$result = $row['Forename'].' '.$row['Surname'].' '.$row['DOB'].' '.$row['Address1'].' '.$row['Address2'].' '.$row['Postcode'];
$bold_word='<b>'.$search_word.'</b>';
$final_msg = str_ireplace($search_word, $bold_word, $result);
?>

<li><?php echo $final_msg; ?></li>
<?php
}
}
else
{

echo "<li>No Results</li>";

}


}
?>

这是连接到数据库并提取结果。然后我有我的html等......:

脚本:

$(function() {
//-------------- Update Button-----------------


$(".search_button").click(function() {
    var search_word = $("#search_box").val();
    var dataString = 'search_word='+ search_word;

    if(search_word=='')
    {
    }
    else
    {
    $.ajax({
    type: "GET",
    url: "searchdata.php",
    data: dataString,
    cache: false,
    beforeSend: function(html) {

    document.getElementById("insert_search").innerHTML = '';

    $("#flash").show();
    $("#searchword").show();
     $(".searchword").html(search_word);
    $("#flash").html('<img src="ajax-loader.gif" align="absmiddle">&nbsp;Loading Results...');



            },
  success: function(html){
    $("#insert_search").show();
    $("#insert_search").append(html);
    $("#flash").hide();
   // $("#MainTable").append(data);
    if(html.length > 0)
            {
                $("#MainTable tr:not(:first-child)").remove();
            }
            for(var i = 0; i < html.length; i++)
            {
                var date = new Date(html[i]["Timestamp"]*1000);
                html[i]["Timestamp"] = date.getHours()+":"+date.getMinutes();
                $("#MainTable").append("<tr><td><a href='#' id='info"+data[i]["ID"]+"' data-role='button' data-theme='b' data-icon='check' data-iconpos='notext' class='id'></a></td><td>"+data[i]["Timestamp"]+"</td><td>"+data[i]["ID"]+"</td><td>"+data[i]["Forename"]+" "+data[i]["Surname"]+"</td><td class='hidden'>"+data[i]["ID"]+"</td><td>"+data[i]["DOB"]+"</td><td class='hidden'>"+data[i]["Address2"]+"</td><td class='hidden'>"+data[i]["Town"]+"</td><td class='hidden'>"+data[i]["City"]+"</td><td class='hidden'>"+data[i]["County"]+"</td><td>"+data[i]["Address1"]+"</td><td>"+data[i]["Postcode"]+"</td><td class='hidden'>"+data[i]["Phone2"]+"</td><td class='hidden'>"+data[i]["DOB"]+"</td></tr>");                                
                if(data[i]["Completed"] == "1")
                {
                    $("#MainTable tr:last-child td").addClass("lineThrough");
                }
                $("#MainTable tr:last td:first a, #MainTable tr:last td:last a").button();

}




  }
});

    }




    return false;
    });
//---------------- Delete Button----------------


});

我尝试了一些不同的事情,例如将表脚本中的 +Data+ 更改为 html 或尝试添加 append(html) 但我没有运气,当我搜索时,我的表中没有定义,或者它完全是空白的。

这是我的表格的 HTML:

<table data-role="table" id="MainTable" data-mode="columntoggle">
                    <tr><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>ID Number</th><th>Name</th><th>DOB</th><th>Address</th><th>Postcode</th></tr>
                    <tr><td colspan='7'>No data currently available, connect to the internet to fetch the latest appointments.</td></tr>
            </table>        

对此有任何帮助将不胜感激。希望这是有道理的。

4

2 回答 2

1

Instead of outputting this in your php page:

 while($row=mysql_fetch_array($sql))
 {
 $result = $row['Forename'].' '.$row['Surname'].' '.$row['DOB'].' '.$row['Address1'].' '.$row['Address2'].' '.$row['Postcode'];
 $bold_word='<b>'.$search_word.'</b>';
 $final_msg = str_ireplace($search_word, $bold_word, $result);
 ?>

 <li><?php echo $final_msg; ?></li>

Loop through the results outputting them in the table format you need, e.g:

echo "<tr>";
echo "<td>".$row['Forename']."</td>"; 
echo "<td>".$row['Surname']."</td>";
//etc
echo "</tr>"

Then take that response and fill the containing table with (jquery AJAX) something like:

 .done(function( response ) {
    $('#MainTable').html(response);
 }
于 2013-08-02T11:59:27.027 回答
0

变化:

if($count > 0)
{
    while($row=mysql_fetch_array($sql))
    {
    $result = $row['Forename'].' '.$row['Surname'].' '.$row['DOB'].' '.$row['Address1'].' '.$row['Address2'].' '.$row['Postcode'];
    $bold_word='<b>'.$search_word.'</b>';
    $final_msg = str_ireplace($search_word, $bold_word, $result);
    ?>
    <tr>
        <td>&nbsp;</td>
        <td><?=$row['id']?></td>
        <td><?=$final_msg?></td>
        <td><?=$row['address']?></td>
        <td><?=$row['postcode']?></td>
    </tr>

    <?php
    }
}
else
{

    echo "<li>No Results</li>";

} 

JS的变化

$.ajax({
    type: "GET",
    url: "searchdata.php",
    data: dataString,
    cache: false,
    beforeSend: function(html) {
        document.getElementById("insert_search").innerHTML = '';

        $("#flash").show();
        $("#searchword").show();
        $(".searchword").html(search_word);
        $("#flash").html('<img src="ajax-loader.gif" align="absmiddle">&nbsp;Loading Results...');
    },
    success: function(html){
        $("#insert_search").show();
        $("#insert_search").append(html);
        $("#flash").hide();
        $("#MainTable").append(html); //<-- append the data from the ajax
    }
});
于 2013-08-02T12:06:00.773 回答