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条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大