du521521521 2012-03-19 20:35
浏览 27
已采纳

如何在html标签,javascript和php之间传递变量[关闭]

I have several divs, which are made automatically by php script. Each div have and record from mysql database in it. And hidden input with value of unique ID. What I wan't to do is when I click on each of this divs, I would like to open overlay with div in middle and use this id to acces another data in database. I have working overlay. I just don't know how to pass that variable.

It's something like this:

echo "<div class='friend'>".$a["username"]."<input type='hidden' value='$id'/>"."</div>";

I hope someone will understand what I'm asking for. :) I know it's terrible explanation.

  • 写回答

3条回答 默认 最新

  • dongshan7060 2012-03-19 20:43
    关注

    Your clean code should be this:

    echo '<div class="friend"> ' . $a["username"] . '<input type="hidden" value="' . $id . '"/></div>';
    

    But way more cleaner would be having a list of your friends like this:

    echo '<ul>';
    echo '<li><a href="#' . $id . '" class="friend">' . $a["username"] . '</a></li>';
    echo '</ul>';
    

    When using jQuery for example you can handle click events with a few lines and pass your user id to an external PHP like this:

    $('.friend').click(function(e) {
      e.preventDefault();
    
      $.get('/userDetails.php?uid=' + $(this).attr('href'), function(data) {
        alert(data);
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿