搜索后自动显示数据

时间:2014-01-27 16:27:21

标签: javascript php html mysql

我希望我的代码可以做的是,如果我搜索批处理代码,等于该批处理代码的数据将自动显示在texbox中,基本上每个批处理代码在每个类别中都有(4)数据,这就是为什么我有(4)文本框.. 。你可以看到我将文本框与php代码分开,因为我有计划并为每个texbox使用javascript。

在我当前的代码中它不起作用,我得到一个错误,上面写着:“注意:未定义的索引:第19行的C:\ xampp \ htdocs \ test \ index.php中的代码”

我目前的代码:

<?php 
ob_start();
session_start();
?>
<script type="text/javascript" src="jquery/jquery.js"> </script>
<script type="text/javascript" src="jqueryui/js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript"  src="Zebra_Dialog-master/public/javascript/zebra_dialog.js"></script>
<link rel="stylesheet" href="Zebra_Dialog-master/public/css/default/zebra_dialog.css" type="text/css">
<link type="text/css" href="jqueryui/css/ui-lightness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" />
<?php
include('include/connect.php');
$batchcode = $_POST['code'];
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode LIKE '$batchcode'");
while($rows = mysql_fetch_array($sql)){
    $aic[] = $rows['aic'];
    $name[] = $rows['name'];
    $address[] = $rows['address'];
}
?>
<html>
<head>
<title>test</title>
</head>
<body>
Search Batchcode:<input type="text" name="code" id="query" /><br />
<form>
<table>
<tr>
<td>
aic: <br />
<input type="text" value="<?php echo $aic[0] ?>" /> <br />
<input type="text" value="<?php echo $aic[1] ?>" /> <br />
<input type="text" value="<?php echo $aic[2] ?>" /> <br />
<input type="text" value="<?php echo $aic[3] ?>" /> <br />
</td>
<td>
Name List: <br />
<input type="text" value="<?php echo $name[0] ?>" /> <br />
<input type="text" value="<?php echo $name[1] ?>" /> <br />
<input type="text" value="<?php echo $name[2] ?>" /> <br />
<input type="text" value="<?php echo $name[3] ?>" /> <br />
</td>
<td>
Address: <br />
<input type="text" value="<?php echo $address[0] ?>" /> <br />
<input type="text" value="<?php echo $address[1] ?>" /> <br />
<input type="text" value="<?php echo $address[2] ?>" /> <br />
<input type="text" value="<?php echo $address[3] ?>" /> <br />
</td>
</form>
<!--search function code-->
<script type="text/javascript">
$(document).ready(function(){

    $("#query").autocomplete({
        source : 'search.php',
        select : function(event,ui){
            $("#query").html(ui.item.value);
        }
    });

});
</script>
</body>
</html>

search.php代码:

<?php

$q = $_GET['term'];

mysql_connect("localhost","root","");
mysql_select_db("test");
$query = mysql_query("SELECT batchcode FROM tb_app WHERE batchcode LIKE '$q%'");

$data = array();
while($row = mysql_fetch_array($query)){
$data[]=array('value'=>$row['batchcode']);
}
echo json_encode($data);
?>

我希望它能够在搜索并点击搜索文本框中的特定批次代码后,数据将自动显示在文本框中。

1 个答案:

答案 0 :(得分:0)

Woah Woah Woah Chappy,直接将POST数据发送给sql查询大不了..

另一方面,您的错误是由于Global Post数组没有名为“code”的数组索引引起的。主要是因为尚未向页面发送任何帖子数据。

您需要将所有内容包装在if语句中。

if(isset($_POST['code'])){
///Logic. 

我会首先阅读一些有关mysql注入的信息,然后再对数据进行清理。