实时搜索功能,如何将结果发送到输入框


Live search function, how to make result to the input box

if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
$query = 'SELECT * FROM User WHERE email LIKE "%'.$search_string.'%"';
// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
    $result_array[] = $results;
}
// Check If We Have Results
if (isset($result_array)) {
    foreach ($result_array as $result) {
        // Format Output Strings And Hightlight Matches
        $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['firstName']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['lastName']);
        //$display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';
        // Insert Name
        $output = str_replace('nameString', $display_name, $html);
        // Insert Function
        $output = str_replace('functionString', $display_function, $output);
        // Insert URL
        //$output = str_replace('urlString', $display_url, $output);
        // Output
        echo($output);
    }
}else{
    // Format No Results Output
    $output = str_replace('urlString', 'javascript:void(0);', $html);
    $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
    $output = str_replace('functionString', 'Sorry :(', $output);
    // Output
    echo($output);
}
}

这是一个实时搜索功能。 搜索电子邮件地址,并显示匹配的名字和姓氏。我有两个问题。

1:当我输入@时,它不会给我任何结果。

2:我希望我可以单击结果并放入输入框,但现在它除了重新打开页面外什么也没做。

3:我只想搜索@.之前的电子邮件地址。 喜欢 text@hotmail.com。我只需要搜索文本,不需要搜索 hotmail.com。我怎么能做到这一点。电子邮件在数据库中

在输入框中键入@时此部分的问题

    foreach ($result_array as $result) {
    // Make Sure you have @ in your $result['firstName']
    $display_function = preg_replace( 
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['firstName']);
    // Make Sure you have @ in your $result['lastName']
    $display_name = preg_replace(
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['lastName']);

// Insert Name
$output = str_replace('nameString', $display_name, $html);
// Insert Function
$output = str_replace('functionString', $display_function, $output);
// Insert URL
        //$output = str_replace('urlString', $display_url, $output);
        // Output
        echo($output);
    }

如果主题参数是数组,则返回 preg_replace(( 数组,否则返回字符串。

看看 preg_replace(( 函数

 If matches are found, the new subject will be returned, otherwise subject  
 will be returned unchanged or NULL if an error occurred.