douwen9540 2013-09-09 15:59
浏览 65
已采纳

将特定PHP变量发送回jQuery的正确方法?

I'm kinda confused how to solve this problem in a right way..

This is jQuery function I intend to use. Runs on 1.7.2 jQuery, PHP 5.2.

    $(function() {
        var timer = setInterval( showDiv, 5000);

//var categoryid = ;??

        function showDiv()
        {

        $.post("check4_new_data.php?categoryid="+encodeURIComponent(categoryid),
        function(response)
        {
            $('#awaiting').fadeOut(400); //cosmetics, nothing important
            $('#posting1').html(unescape(response));
            $('#posting1').fadeIn(200);
        });

       }
    });

Question:

There is a PHP variable $categoryid set when PHP page loads and.. this function needs it. So, how to send $categoryid back to jQuery in a right, safest way?

Note:Ive been using cookie as first test to keep categoryid present for PHP, but then all opened (and different) pages started to load up same data (according to latest page visited).

Thx!

  • 写回答

3条回答 默认 最新

  • drdyf42880 2013-09-09 16:05
    关注

    You could do it like this:

    <div id="posting1" data-category="my_category">
        ...
    </div>
    

    This way, your markup and JavaScript can be separated completely. So you can fetch the category for the related post you are trying to refresh:

    var category = $('#posting1').attr('data-category');
    $.post(
        "check4_new_data.php?categoryid="+encodeURIComponent(category), 
        function (response) {
            $("#posting1").html(response);
        }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程