我是PHP,ajax和mysql的新手。我正在尝试构建一个 Web 应用程序,在其中从我的数据库中获取输出表。我的问题是,如果我想使用当前输出表搜索另一个表,我应该使用什么代码,例如
名 姓 约翰·史密斯
是我的出放表,如果我单击史密斯,它应该搜索包含有关史密斯数据的其他表格
我的 js 代码是
$(function myFunction() {
$("#lets_search").bind('submit',function() {
var value = $('#str').val();
var value1= $('#str1').val();
$.post('test_refresh.php',{value:value,value1:value1}, function(data){
$("#search_results").html(data);
});
return false;
});
});
我的 Php 代码是
<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("cancer database") or die(mysql_error());
echo"";
/*$query = mysql_query("SELECT * FROM tbl_cancer_database
WHERE gene_symbol LIKE '".$_POST['value']."'
OR gene_name LIKE '".$_POST['value']."'
OR gene_id LIKE '".$_POST['value']."'
OR gene_locus LIKE '".$_POST['value']."'
OR function LIKE '%".$_POST['value']."%'
OR alteration_in_cancer LIKE '".$_POST['value']."'
OR reference LIKE '".$_POST['value']."'
");*/
$query = mysql_query("SELECT * FROM tbl_cancer_database
WHERE gene_name LIKE '".$_POST['value']."'
and gene_id LIKE '".$_POST['value1']."'
");
echo '<br />';
echo "You have searched for ".$_POST['value']." and ".$_POST['value1']."";
echo '<br />';
echo '<br />';
echo '<table>';
echo "<tr>
<th bgcolor=silver>Sr. No.</th>
<th bgcolor=silver>Gene Symbol</th>
<th bgcolor=silver>Gene Name</th>
<th bgcolor=silver>Gene Id</th>
<th bgcolor=silver>Gene locus</th>
<th bgcolor=silver>Function</th>
<th bgcolor=silver>Alteration in cancer</th>
<th bgcolor=silver>Reference</th></tr>";
while ($data = mysql_fetch_array($query)) {
echo '<tr style="background-color:pink;">
<td>'.$data["id"].'</td>
<td>'.$data["gene_symbol"].'</td>
<td><a href="http://www.ncbi.nlm.nih.gov /gene/?term='.$data["gene_name"].'">'.$data["gene_name"].'</a></td>
<td>'.$data["gene_id"].'</td>
<td>'.$data["gene_locus"].'</td>
<td>'.$data["function"].'</td>
<td>'.$data["alteration_in_cancer"].'</td>
<td>'.$data["reference"].'</td>
</tr>';
}
echo '</table>';
?>
任何帮助将不胜感激。
首先添加一个唯一标识每个表数据的类。
所以你的桌子看起来像这样
<table>
<tr>
<td class="fistname">John</td>
<td class="lastname">Smith</td>
</tr>
</table>
那么你的javascript看起来像这样
$(function myFunction() {
$("#lets_search").bind('submit',function() {
var value = $('#str').val();
var value1= $('#str1').val();
$.post('test_refresh.php',{value:value,value1:value1}, function(data){
$("#search_results").html(data);
initializeClick();
});
return false;
});
});
function initializeClick(){
$('.firstname').click(function(){
var sSearchValue = $(this).text();
$.post('PAGE TO SEARCH YOUR OTHER TABLE',{value:sSearchValue}, function(data){
//CAN DO WHAT EVER YOU WANT WITH THE RESULTS HERE
});
});
}
还要记住,当字符在 html 上输出时要对其进行转义