douqiao7958 2017-02-17 05:56
浏览 84

Javascript:如何从$ _SESSION数组中打印值?

Is there a way to have javascript create php code, to be executed when it runs on the php page? I'm trying to set the value of an input to , where div_id is a js variable, and inputs is a 2D associative array.

The problem is that it literally sets the value to "" instead of the value of the floor_type field. Everything else seems to work.

inputs_div.innerHTML += `<div id="`+div_id+`" class="dynamic_div">
                        <ul class="dynamic_ul">
                        <li><input type="text" onblur="$('.save').click();" placeholder="Flooring Type: Wood Floor, Tile, etc." name="inputs[`+div_id+`][floor_type]" maxlength="255" value="<?php echo $_SESSION['inputs']['`+div_id+`']['floor_type']; ?>"></li>
                        </ul>
                        </div>

I'm creating some html inputs dynamically with javascript, but I'm looking for a way to prevent them from getting cleared if the page is refreshed. Maybe there is a simpler way to make the inputs stay there?

My idea was to use ajax to run some php code from the onblur event of each input element, which saves the inputs[][] array into the session, so it doesn't get lost when refreshing the page.

  • 写回答

1条回答 默认 最新

  • dongyin5516 2017-02-17 06:11
    关注

    Use a hidden field to store $_SESSION[] value and then on document ready get the value of hidden field and set to the javascript variable.

    <input type="hidden" id="hdn" value="<? php echo $_SESSION['inputs'] ?>" />
    
    
    $(document).ready(function()
    {    var session_value = $('#hdn').attr('value');    // now use session_value variable when set the innerhtml of inputs_div });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接