如何在第一次按键时从数据库中获取数据

i have a text field

<input type="text" class="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4">

when i pressed key it will call a function i js

function vhc_record()
{
var  data='vhc='+document.getElementById('txt_sh_vid').value;
 loadXMLDoc('ship/vehicle_ship/','div_vhc',data);
   document.getElementById('div_vhc').style.visibility="visible";
}

it will take the text field value and search a result from database

<?php 
$vhc=$_POST['vhc'];
$con=mysql_connect("localhost","root","");
if(!$con)
{
die ("error".mysql_error());
}
$db_found=mysql_select_db("oil",$con);
if($db_found)
{


$qry="select * from vehicle where vehiclenumber like '$vhc%'";
$result=mysql_query($qry);
echo "<table class='select'>
<tr>
<th>id</th>
<th>vehiclenum</th>
<th>capacity</th>
<th>owner</th>

</tr>
";

while($r = mysql_fetch_assoc($result)){

    echo"<tr onclick='vhc_inv()'>";
    echo"<td id='vhcid'>" .$r['vehicleid']."</td>";
echo"<td id='vhcl'>" .$r['vehiclenumber']."</td>";
echo"<td>".$r['capacity']."</td>";
echo"<td>".$r['owner']."</td>";
echo "</tr>";}
echo "</table>";


}


mysql_close($con);

the orignal problem is that it will show the whole table on first keypress and on second key press it will take the firstkeypressed value as a search let suppose when i write n in textfield it will show the whole vehicle table and when i pressed k then it will show only the record having vehicle number starting from n.and when i pressed any other letter then it will show only the record having vehiclenumber=nk *i want it that when i press n in text field it will only show the record of vehicles having vehicle number starting from n*

查看全部
dopuz8728
dopuz8728
2011/03/23 14:57
  • php
  • html
  • ajax
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复