0

I am new to php, ajax and mysql. I am trying to build a web application where i get an output table from my database. 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

my js code is

$(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
$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.

4

1 回答 1

0

您只需要以适当的方式绑定事件:

$(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 注入漏洞。

于 2013-10-11T16:59:51.790 回答