doudang1052 2016-04-29 23:54
浏览 397
已采纳

如何从ajax实时搜索中获取HTML文本框的值?

I have already completed the live search part. I want to put the value of the search result into a textbox in a different php file.

AJAX Code: [In index.php]

<script>
function search(string){
        var xmlhttp;
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else{
            xmlhttp = new ActiveXObject("XMLHTTP");
        }
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                document.getElementById("search_div").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "search.php?s="+string, true);
        xmlhttp.send(null);
    }
</script>

html text type: [In index.php]

<div class="input-field col s12">
                <i class="material-icons prefix">account_circle</i>
                <input id="name" type="text" name="name" class="validate" onkeyup="search(this.value)">
                <label for="name">Doctor's Name</label>
                <div id="search_div">
              </div>

Data Retrieve from Database: [in search.php]

<?php
ob_start();
require 'config.php';
$conn = new mysqli("localhost","root","","dcare");

if($conn->connect_errno)
{
    die('Sorry! Connection was not Successful');
}

if(isset($_GET['s']) && $_GET['s'] != ''){
    $s = $_GET['s'];
    $sql = "SELECT * FROM doctor WHERE name LIKE '%$s%'";
    $result = $conn->query($sql) or die($conn->error);
    while($row = $result->fetch_assoc()){
        $name = $row['name'];
        $designation = $row['designation'];
        echo "<div style='' id='searchtitle'>" . "<a style='font-family: verdana; text-decoration: none; color: black; href='$name'>" . $name . "</a>" . "</div>";
    }

}

?>

Now I want to Click on the Tania Rahman and The value will be saved to the value of "name" textbox in the index.php file.

  • 写回答

1条回答 默认 最新

  • douwen5833 2016-05-01 00:31
    关注

    So based upon what you have said, all you need is some simple javascript. However to make it a bit tidier, I have modified your script below.

    AJAX Code in index.php, just added an additional function.

    <script>
    
    function search(string){
            var xmlhttp;
            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            }else{
                xmlhttp = new ActiveXObject("XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    document.getElementById("search_div").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "search.php?s="+string, true);
            xmlhttp.send(null);
    }
    
    function setName(string) {
    
        document.getElementById("name").value = string;
    }
    
    </script>
    

    HTML Code in index.php (not updated)

    <div class="input-field col s12">
        <i class="material-icons prefix">account_circle</i>
        <input id="name" type="text" name="name" class="validate" onkeyup="search(this.value)">
        <label for="name">Doctor's Name</label>
        <div id="search_div">
    </div>
    

    Search.php

    <?php
    ob_start();
    require 'config.php';
    $conn = new mysqli("localhost","root","","dcare");
    
    if($conn->connect_errno)
    {
        die('Sorry! Connection was not Successful');
    }
    
    if(isset($_GET['s']) && $_GET['s'] != ''){
        $s = $_GET['s'];
        $sql = "SELECT * FROM doctor WHERE name LIKE '%$s%'";
        $result = $conn->query($sql) or die($conn->error);
        while($row = $result->fetch_assoc()){
            $name = $row['name'];
            $designation = $row['designation'];
    
            // No need for a href in the anchor tag, as you don't want it to redirect to any page, but added a style for a pointer cursor
            echo "<div id='searchtitle'>" . "<a style='font-family: verdana; text-decoration: none; color: black; cursor: pointer;' onclick='setName(\"$name\")'>" . $name . "</a>" . "</div>";
        }
    
    }
    
    ?>
    

    I haven't tested it - but it should work provided your script was already working.

    A couple of notes:

    1. You should avoid using inline styles (ie. <a style="">), as whilst it is a quick and easy solution it makes it harder to maintain your code. Define a style class that has the same attributes. One of the purposes of CSS is to seperate styling from semantics.
    2. You should take a look at the alternative syntax for control structures (like if, while etc). It helps keep your HTML tidy when outputting from PHP and avoids the need to do things like echo etc. See the following from the PHP Manual: http://php.net/manual/en/control-structures.alternative-syntax.php

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵