2014-01-27 104 views
0

有誰知道如何在不使用提交按鈕的情況下搜索和顯示數據。因爲這是我需要完成我的代碼的唯一功能。任何人都可以幫助我嗎?我不想使用提交按鈕,因爲它刷新頁面...我已經有搜索的代碼,我需要一個函數,我搜索後,數據將顯示在文本框中,而不使用提交按鈕。在不使用提交按鈕的情況下在搜索中顯示數據

PHP代碼:

<?php 
if($_GET){ 
include('include/connect.php'); 
$batchcode = $_GET['code']; 
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode = '".$batchcode."' "); 
if($sql) { 
    while($rows = mysql_fetch_array($sql)){ 
     $aic[] = $rows['aic']; 
     $name[] = $rows['name']; 
     $address[] = $rows['address']; 
    } 
} 
}else{ 
    $aic  = array(NULL,NULL,NULL,NULL); 
    $name = array(NULL,NULL,NULL,NULL); 
    $address = array(NULL,NULL,NULL,NULL); 
    } 
?> 

的html代碼:

<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<form action="" method="get"> 
Search Batchcode:<input type="text" name="code" id="query" /><br /> 
<table> 
<tr> 
<td> 
aic: <br /> 
<input type="text" name="optA1" value="<?php if(empty($aic[0])){$aic[0] = array(NULL);}else{echo $aic[0];} ?>" /> <br /> 
<input type="text" name="optA2" value="<?php if(empty($aic[1])){$aic[1] = array(NULL);}else{echo $aic[1];} ?>" /> <br /> 
<input type="text" name="optA3" value="<?php if(empty($aic[2])){$aic[2] = array(NULL);}else{echo $aic[2];} ?>" /> <br /> 
<input type="text" name="optA4" value="<?php if(empty($aic[3])){$aic[3] = array(NULL);}else{echo $aic[3];} ?>" /> <br /> 
</td> 
<td> 
Name List: <br /> 
<input type="text" name="optB1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" /> <br /> 
<input type="text" name="optB2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" /> <br /> 
<input type="text" name="optB3" value="<?php if(empty($name[2])){$name[2] = array(NULL);}else{echo $name[2];} ?>" /> <br /> 
<input type="text" name="optB4" value="<?php if(empty($name[3])){$name[3] = array(NULL);}else{echo $name[3];} ?>" /> <br /> 
</td> 
<td> 
Address: <br /> 
<input type="text" name="optC1" value="<?php if(empty($address[0])){$address[0] = array(NULL);}else{echo $address[0];} ?>" /> <br /> 
<input type="text" name="optC2" value="<?php if(empty($address[1])){$address[1] = array(NULL);}else{echo $address[1];} ?>" /> <br /> 
<input type="text" name="optC3" value="<?php if(empty($address[2])){$address[2] = array(NULL);}else{echo $address[2];} ?>" /> <br /> 
<input type="text" name="optC4" value="<?php if(empty($address[3])){$address[3] = array(NULL);}else{echo $address[3];} ?>" /> <br /> 
</td> 
</form> 
</body> 
</html> 

腳本代碼:

<!--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> 

的search.php頁面代碼:

<?php 

$q = $_GET['term']; 

mysql_connect("localhost","root",""); 
mysql_select_db("test"); 
$query = mysql_query("SELECT DISTINCT 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); 
?> 

回答

0

在你的html中,你需要創建一個鏈接或一個簡單的按鈕,並給它一個類或ID。 然後在javascript中添加單擊事件偵聽器並執行ajax調用。

<a href="#" id="mysubmit"> search here </a> 

而且在你的JavaScript

$("#mysubmit").click(function() { 
     // read query field value 
     // Perform ajax call 
}); 

希望這會幫助你。

0

你也可以這樣做:

$("#query").change(function() { 
    var sendMe = $.post("search.php"); 
    sendMe.done(function(data) { 
     $("#query").html(data); 
    }); 
}); 
相關問題