开发者

how to get the data from data base on first key press

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 pro开发者_JAVA百科blem 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*


Try to use onKeyUp event instead of onKeyPress

for example: <input type="text" class="text" id="txt_sh_vid" onKeyUp="vhc_record()" maxlength="4">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜