doujiushi9007 2017-02-19 17:13
浏览 47
已采纳

将带有数据值的列表中的值传递给mysql查询

I have a list

<ul id="my_ul">
<li data-value="1"><a href="test.php">111</a></li>
<li data-value="2"><a href="test.php">112</a></li>
<li data-value="3"><a href="test.php">113</a></li>
</ul>

I tried to pass the data-value on click event to MySQL query. I tried it with jQuery but when i var_dump the result its NULL.

Here is how i am getting the value

$('#my_ul').on("click", "li", function(){
   $.ajax("fetch_test.php",{method:"post", data:{val:$(this).attr("data-value")}}); 
});

and here is the fetch_test.php file code :

function showValue(){
include_once "connect.php";
$test=$_SESSION['val'];
$query="SELECT * FROM product INNER JOIN smetka on smetka.id=product.smetka_id WHERE product.smetka_id=$test";
$result=mysqli_query($conn,$query);
    $alert="";
        while($row=mysqli_fetch_array($result)){
            $alert .="<li data-value='$row[id]'><a href='test.php'>$row[name]";

            $alert .= "</a></li>";
        }
    return $alert;
}

How can i pass the data-value from the li?

  • 写回答

2条回答 默认 最新

  • douduo2407 2017-02-19 17:19
    关注

    You should bind the event handler with anchor element and use event.preventDefault() to cancel its the default action. You can use .closest() to traverse up to li element.

    $('#my_ul').on("click", "li a", function(e) {
      //Cancels default action of anchor
      e.preventDefault();
      var value = $(e.target).closest('li').data("value");
      console.log(value);
      $.ajax({url: "fetch_test.php", method:"post", data:{val:value}});
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul id="my_ul">
      <li data-value="1"><a href="test.php">111</a></li>
      <li data-value="2"><a href="test.php">112</a></li>
      <li data-value="3"><a href="test.php">113</a></li>
    </ul>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求有缘人帮我把笛卡尔坐标系转换为经纬度 有偿
  • ¥15 vue2中使用计算属性
  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集
  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33