简体   繁体   English

如何将表数据类从php传递到ajax / javascript

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

I am new to php, ajax and mysql. 我是php,ajax和mysql的新手。 I am trying to build a web application where i get an output table from my database. 我正在尝试构建一个Web应用程序,从数据库中获取输出表。 for eg 例如

name surname john smith 姓约翰·史密斯

is my out put table, if i click on smith it should search other table containing data about smith. 是我的输出表,如果我单击史密斯,它应该搜索其他包含史密斯数据的表。 I tried assigning a class to and calling it from java script but it's not working 我尝试将一个类分配给Java脚本并从Java脚本调用它,但是它不起作用

my js code is 我的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);
     });
   });
   });

and my php code is 和我的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>';


?>

Any help would be appreciated. 任何帮助,将不胜感激。

You just need to bind the events in a proper way : 您只需要以适当的方式绑定事件:

$(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);
        });
    });
});

and try sanitizing query parameters to avoid SQL injection vulnerabilities. 并尝试清理查询参数以避免SQL注入漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM