将值传递给隐藏的输入?

时间:2015-09-27 07:12:46

标签: php jquery ajax

大家好我无法通过实时搜索传递值,其中搜索的值通过ul标签显示在页面上。实时搜索工作正常。

这是我的数据库查询。(insertdemo.php)

<?php
define('DB_USER', 'root');
define('DB_PASSWORD', 'thartpc');
define('DB_SERVER', 'localhost');
define('DB_NAME', 'hospital');

if (!$db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME)) {
die($db->connect_errno.' - '.$db->connect_error);
}

$arr = array();

if (!empty($_POST['keywords'])) {
$keywords = $db->real_escape_string($_POST['keywords']);
$sql = "SELECT id, patientid, lastname, firstname, middlename FROM patient WHERE lastname LIKE '%".$keywords."%'";
$result = $db->query($sql) or die($mysqli->error);
if ($result->num_rows > 0) {
    while ($obj = $result->fetch_object()) {
        $arr[] = array('id' => $obj->id, 'patientid' => $obj->patientid, 'lastname' => $obj->lastname,'firstname' => $obj->firstname, 'middlename' => $obj->middlename);
    }
}
}
echo json_encode($arr);

ajax和jquery代码(index.php)

我只是想将patientid值传递给像这样的输入&lt;。输入类型=&#34;隐藏&#34; ID =&#34; PID&#34;名称=&#34; PID&#34;值=&#34; $ PID&#34;&GT;

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#keyword').on('input', function() {
        var searchKeyword = $(this).val();
        if (searchKeyword.length >= 1) {
            $.post('insertdemo.php', { keywords: searchKeyword },   function(data) {
                $('ul#content').empty()
                $.each(data, function() {
                    $('ul#content').append('<li><a href="inserting.php?id=' + this.id + '">' + this.patientid + ' ' + this.lastname + ' ' + this.firstname + ' ' + this.middlename + '</a></li>');
                });
            }, "json");
        }
    });
    });
   </script>

html部分

<div class="panel panel-default" style="background-color:#2B547E; border- color:#2B547E">
<div class="panel-body">
<div class="container">                         
<div class="col-md-6" style="width:400px; padding-left:0px">    
<form role="form" method="POST"> 
<div class="btn-group btn-group" style="margin-left:0px" role="group" aria-label="...">
<div class="btn-group" role="group">
<button type="button" class="btn btn-primary btn-md">Pharmacy  Portal</button>
</div>
                        <div class="btn-group" role="group">         
                            <input type="text" id="keyword" class="form-control" placeholder="Enter Patient ID">
                        </div>
                        <div class="btn-group" role="group">         
                            <button class="btn btn-default btn-md" style="height:34px; width:60px ;color:#2B547E " name="submit" type="submit"><span class="glyphicon glyphicon-search" ></span></button>
                        </div>
                    </div>          
                </form>
            </div>


            <div class="col-md-6 pull-right" style="padding-right:0px"> 
                <div class="btn-group pull-right" role="group">
                        <div class="dropdown">
        <a class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
          <span class="glyphicon glyphicon-align-justify"></span>
          <span class="icon-bars-button">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </span>
        </a>
        <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dropdownMenu1">
          <li role="presentation"><a role="menuitem" tabindex="-1" href="index.php">Home</a></li>
          <li role="separator" class="divider"></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="">Patient</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="">Billing</a></li>

        </ul>
    </div>
                </div>

            </div>
        </div>
    </div>
</div>
 <?php $pid = ''; ?>
<div class="container">
    <ul id="content"></ul>
    <input type="text" name="pid" id="pid" class="form-control" value="<?php "$pid" ?>">

</div>

2 个答案:

答案 0 :(得分:0)

将以下行添加到您的html部分

<input type="hidden" id="pid" name="pid" value=""/>

在Jquery中,更新隐藏值

$("#pid").val(patientid);

答案 1 :(得分:0)

你可以试试这个:

$(document).ready(function() {
    $('#keyword').on('input', function() {
        var searchKeyword = $(this).val();
        if (searchKeyword.length >= 1) {
            $.post('insertdemo.php', { keywords: searchKeyword },   function(data) {
                $('ul#content').empty();
                if (data.length == 1)
                    $('#pid')
                        .val(data[0].patientid);
                $.each(data, function() {
                    $('ul#content').append('<li><a href="inserting.php?id=' + this.id + '">' + this.patientid + ' ' + this.lastname + ' ' + this.firstname + ' ' + this.middlename + '</a></li>');
                });
            }, "json");
        }
    });
});