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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘