dqx36753 2014-04-09 14:36
浏览 19
已采纳

如何将变量从jQuery传递给PHP? [重复]

This question already has an answer here:

I have a jQuery function, which, on click, itercepts a clicked link:

$("a").click(function(e) {
    e.preventDefault();
    var link = $(this).prop('href'); 
    alert(link);
});

and it works properly, but how to pass that link to a PHP variable to 'echo' it later?

I unsuccessfully tried:

$("a").click(function(event) {
    event.preventDefault();
    var link = $(this).prop('href'); 
    <?php $link = .'link'. ?> 
});

I know it may be a silly question, I looked all over the web but I didn't find any proper answer.

</div>
  • 写回答

2条回答 默认 最新

  • duanquan4451 2014-04-09 14:48
    关注

    The only way to do this is to have a script wich stores values passed to it in a session or to a DB and either output the session data later or read from the db

    $("a").click(function(event) {
        event.preventDefault();
        var link = $(this).prop('href'); 
        $.post('saveLink.php', {href : link}); 
    });
    

    the saveLink.php would be something like this

    <?php
       // Clean your POST variable, always clean any POST variables
       // see here for a good discussion on that
       // http://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function
       $link = htmlspecialchars($_POST['href']);
    
       $_SESSION['links'] = $link;
    ?>
    

    then later on you can retrieve all you session data

    Note

    Session data is not permanent and not shareable between users so if you need to do that you would be better off with a db query also if you need to access a particular link then you will need to send some sort of id with it.

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

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划