如何将表数据类从PHP传递给Ajax/JavaScript


how to pass class of table data from php to ajax/javascript

我是php,ajax和mysql的新手。我正在尝试构建一个 Web 应用程序,在其中从我的数据库中获取输出表。 例如

名字 姓 约翰·史密斯

是我的出放表,如果我单击史密斯,它应该搜索包含有关史密斯数据的其他表。我尝试将类分配给并从java脚本调用它,但它不起作用

我的 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);
        initializeClick();
       });
       return false;
    });
  });
$(function initializeClick(){
$(".genename").click(function(){
    var sSearchValue = $(this).text();
     $.post('test_refresh.php',{value:sSearchValue}, function(data){
        $("#search_results").html(data);
     });
   });
   });

我的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 '<h2 class=header>abc</h2>';
echo '<br />';
echo '<br />';
echo '<table>';
echo "<tr>
<th bgcolor=silver>Sr. No.</th> 
<th bgcolor=silver>Gene Symbol</th>
<th bgcolor=silver class='genename'>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 class="id">'.$data["id"].'</td>
    <td class="genesymbol">'.$data["gene_symbol"].'</td>
    <td class="genename">'.$data["gene_name"].'</td>
    <td class="geneid">'.$data["gene_id"].'</td>
    <td class="genelocus">'.$data["gene_locus"].'</td>
    <td class="function">'.$data["function"].'</td>
    <td class="alteration">'.$data["alteration_in_cancer"].'</td>
    <td class="reference">'.$data["reference"].'</td>
  </tr>';
}
echo '</table>';

?>

任何帮助将不胜感激。

你只需要以正确的方式绑定事件:

$(function() {
    $("#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;
    });
    $(".genename").click(function() {
        var sSearchValue = $(this).text();
        $.post('test_refresh.php', {
            value: sSearchValue
        }, function(data) {
            $("#search_results").html(data);
        });
    });
});

并尝试清理查询参数以避免 SQL 注入漏洞。