dsn5510 2013-04-09 21:00
浏览 32
已采纳

使用jQuery访问更新的php $ _SESSION变量

I have a drop-down menu that uses an onChange event to trigger a mysqli query. The query populates an array with the returned results, which in turn is assigned to a $_SESSION variable.

I am trying to use the array stored in the $_SESSION variable to populate a select box whenever the value in the drop-down menu changes.

This is the relevant JavaScript code:

xmlhttp.open("POST", "getStudentList.php?q="+yearGroup, true); 
xmlhttp.send();


var newStudentList =<?php echo json_encode($_SESSION['studentList']) ?>; 

    // clear select box 
    $('#studentList').empty();          

    // populate select box with JS array items
    $.each(newStudentList, function(key, value) {   
         $('#studentList')
              .append($('<option>', { value : key })
              .text(value));
    });                     

    $('#studentList').selectmenu("refresh",true);

$_SESSION['studentList'] is being updated correctly in 'getStudentList.php', but the updates are not being reflected in the call within the Javascript until the page is reloaded... how can I make the updates happen automatically?

I have checked past posts but haven't found anything that really helps or that I understand! I would be grateful for any assistance - I am a complete novice in using Javascript / Jquery and have cobbled bits of php together from here and there, so please go easy on me. (yes, I am using session_start()!)

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • doubi8383 2013-04-09 21:09
    关注

    You can do something like this,

    $("#studentList").on('change',function() {
        $.ajax({
            url: "getStudentList.php",
            dataType: "json",
            success: function(newStudentList) {
    
               // clear select box 
               $('#studentList').empty();          
    
               // populate select box with JS array items
               $.each(newStudentList, function(key, value) {   
                   $('#studentList').append($('<option>', { value : key }).text(value));
               });
    
               $('#studentList').selectmenu("refresh",true);
    
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?