开发者

How to retrieve html elements value via .click jQuery function with data retrieved from MySQL?

开发者 https://www.devze.com 2023-04-08 21:24 出处:网络
I have a question to ask. I have a search result that is based on data retrieved from MySQL database. And based on the data retrieved, I have embedded and echo a div and <a> tag to know which &

I have a question to ask.

I have a search result that is based on data retrieved from MySQL database. And based on the data retrieved, I have embedded and echo a div and <a> tag to know which <a> is been clicked so that I can use searchresult_details.php to retrieve more details using ajax jQuery.

<?php

//include database connection file to call the class
include('dbconn.php');

//db global connection
$db = new DBConfig();
$db -> config();
$db -> conn();

//retrieve the keyword entered in the form in search.php
$keyword = $_POST['keyword'];

//call searchKeyValue method to retrieve the names whether client or staff
searchKeyValue($keyword);

function searchKeyValue($keyword)
{
    $result = mysql_query("SELECT result_key, result_value FROM keyvalue WHERE result_value LIKE  '%$keyword%'");   

    while($row = mysql_fetch_array($result)){
            //echo $row['result_value'];
            //echo "<br />";
            echo '<div style="background-color:#ffa; padding:20px"><a class="record" value="' . $row['result_value'] . '">' . $row['result_value'] . '</a>' . '</div>';
            echo '<input class="tablename" type="hidden" name="tablename" value="' . $row['result_key'] . '" />';
        }
}
?>

However, the <a> will have multiple rows been echoed. I need to retrieve the <a> that is clicked and the value that it is stored in that parti开发者_运维知识库cular <a> element. I can't be using id in <a> because I think it would be tedious. I was thinking to use class so that in javascript I can use document.getElementsByClassName but I know such function in IE has errors.

Any recommendations?


please check jQuery class selector

http://api.jquery.com/class-selector/


Take a look at this example.

<?php
while($stmt->fetch()){?>

  <td class="other">
      <input type="hidden"  class="rowid"  value="<?php echo $id ?>"/>
      <?php echo round($other,2); ?>
  </td>`enter code here`

<?php } ?>

<script>
//jquery code:
$(document).ready(function(){
    $(".other ").mouseover(function(){
        var rowid = $('#rowid').val();
        $.get('other.php',{postrowid:rowid},
            function(data){
              $('#otherResult').html(data);
              $('#otherResult').show();
              $(".other").mouseout(function(){
                $('#otherResult').hide();
              });
            });
    });
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消